mirror of
https://github.com/krahets/hello-algo.git
synced 2025-08-01 17:43:24 +08:00
deploy
This commit is contained in:
@ -26,7 +26,7 @@
|
||||
|
||||
|
||||
|
||||
<title>3.4 Character Encoding * - Hello Algo</title>
|
||||
<title>3.4 Character encoding * - Hello Algo</title>
|
||||
|
||||
|
||||
|
||||
@ -153,7 +153,7 @@
|
||||
<div class="md-header__topic" data-md-component="header-topic">
|
||||
<span class="md-ellipsis">
|
||||
|
||||
3.4 Character Encoding *
|
||||
3.4 Character encoding *
|
||||
|
||||
</span>
|
||||
</div>
|
||||
@ -201,7 +201,13 @@
|
||||
|
||||
<li class="md-select__item">
|
||||
<a href="/" hreflang="zh" class="md-select__link">
|
||||
中文
|
||||
简体中文
|
||||
</a>
|
||||
</li>
|
||||
|
||||
<li class="md-select__item">
|
||||
<a href="/zh-hant/" hreflang="zh-Hant" class="md-select__link">
|
||||
繁體中文
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@ -393,7 +399,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
0.1 About This Book
|
||||
0.1 About this book
|
||||
</span>
|
||||
|
||||
|
||||
@ -414,7 +420,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
0.2 How to Read
|
||||
0.2 How to read
|
||||
</span>
|
||||
|
||||
|
||||
@ -491,7 +497,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2m0 16H5V5h14v14M6.2 7.7h5v1.5h-5V7.7m6.8 8.1h5v1.5h-5v-1.5m0-2.6h5v1.5h-5v-1.5M8 18h1.5v-2h2v-1.5h-2v-2H8v2H6V16h2v2m6.1-7.1 1.4-1.4 1.4 1.4 1.1-1-1.4-1.4L18 7.1 16.9 6l-1.4 1.4L14.1 6 13 7.1l1.4 1.4L13 9.9l1.1 1Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 1. Introduction to Algorithms
|
||||
Chapter 1. Introduction to algorithms
|
||||
</span>
|
||||
|
||||
|
||||
@ -507,7 +513,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_2">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 1. Introduction to Algorithms
|
||||
Chapter 1. Introduction to algorithms
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -524,7 +530,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.1 Algorithms are Everywhere
|
||||
1.1 Algorithms are everywhere
|
||||
</span>
|
||||
|
||||
|
||||
@ -545,7 +551,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
1.2 What is an Algorithm
|
||||
1.2 What is an algorithm
|
||||
</span>
|
||||
|
||||
|
||||
@ -626,7 +632,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M6 2h12v6l-4 4 4 4v6H6v-6l4-4-4-4V2m10 14.5-4-4-4 4V20h8v-3.5m-4-5 4-4V4H8v3.5l4 4M10 6h4v.75l-2 2-2-2V6Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 2. Complexity Analysis
|
||||
Chapter 2. Complexity analysis
|
||||
</span>
|
||||
|
||||
|
||||
@ -642,7 +648,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_3">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 2. Complexity Analysis
|
||||
Chapter 2. Complexity analysis
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -659,7 +665,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.1 Algorithm Efficiency Assessment
|
||||
2.1 Algorithm efficiency assessment
|
||||
</span>
|
||||
|
||||
|
||||
@ -680,7 +686,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.2 Iteration and Recursion
|
||||
2.2 Iteration and recursion
|
||||
</span>
|
||||
|
||||
|
||||
@ -701,7 +707,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.3 Time Complexity
|
||||
2.3 Time complexity
|
||||
</span>
|
||||
|
||||
|
||||
@ -722,7 +728,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
2.4 Space Complexity
|
||||
2.4 Space complexity
|
||||
</span>
|
||||
|
||||
|
||||
@ -805,7 +811,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M11 13.5v8H3v-8h8m-2 2H5v4h4v-4M12 2l5.5 9h-11L12 2m0 3.86L10.08 9h3.84L12 5.86M17.5 13c2.5 0 4.5 2 4.5 4.5S20 22 17.5 22 13 20 13 17.5s2-4.5 4.5-4.5m0 2a2.5 2.5 0 0 0-2.5 2.5 2.5 2.5 0 0 0 2.5 2.5 2.5 2.5 0 0 0 2.5-2.5 2.5 2.5 0 0 0-2.5-2.5Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 3. Data Structures
|
||||
Chapter 3. Data structures
|
||||
</span>
|
||||
|
||||
|
||||
@ -821,7 +827,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="true">
|
||||
<label class="md-nav__title" for="__nav_4">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 3. Data Structures
|
||||
Chapter 3. Data structures
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -838,7 +844,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.1 Classification of Data Structures
|
||||
3.1 Classification of data structures
|
||||
</span>
|
||||
|
||||
|
||||
@ -859,7 +865,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.2 Fundamental Data Types
|
||||
3.2 Fundamental data types
|
||||
</span>
|
||||
|
||||
|
||||
@ -880,7 +886,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.3 Number Encoding *
|
||||
3.3 Number encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@ -910,7 +916,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.4 Character Encoding *
|
||||
3.4 Character encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@ -921,7 +927,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
3.4 Character Encoding *
|
||||
3.4 Character encoding *
|
||||
</span>
|
||||
|
||||
|
||||
@ -945,7 +951,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#341-ascii-character-set" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.1 ASCII Character Set
|
||||
3.4.1 ASCII character set
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -954,7 +960,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#342-gbk-character-set" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.2 GBK Character Set
|
||||
3.4.2 GBK character set
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -963,7 +969,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#343-unicode-character-set" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.3 Unicode Character Set
|
||||
3.4.3 Unicode character set
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -972,7 +978,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#344-utf-8-encoding" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.4 UTF-8 Encoding
|
||||
3.4.4 UTF-8 encoding
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -981,7 +987,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#345-character-encoding-in-programming-languages" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.5 Character Encoding in Programming Languages
|
||||
3.4.5 Character encoding in programming languages
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -1067,7 +1073,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 5v14h17V5H3m4 2v2H5V7h2m-2 6v-2h2v2H5m0 2h2v2H5v-2m13 2H9v-2h9v2m0-4H9v-2h9v2m0-4H9V7h9v2Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Array and linked list
|
||||
</span>
|
||||
|
||||
|
||||
@ -1083,7 +1089,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_5">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 4. Array and Linked List
|
||||
Chapter 4. Array and linked list
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1121,7 +1127,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
4.2 Linked List
|
||||
4.2 Linked list
|
||||
</span>
|
||||
|
||||
|
||||
@ -1163,7 +1169,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
4.4 Memory and Cache
|
||||
4.4 Memory and cache
|
||||
</span>
|
||||
|
||||
|
||||
@ -1242,7 +1248,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17.36 20.2v-5.38h1.79V22H3v-7.18h1.8v5.38h12.56M6.77 14.32l.37-1.76 8.79 1.85-.37 1.76-8.79-1.85m1.16-4.21.76-1.61 8.14 3.78-.76 1.62-8.14-3.79m2.26-3.99 1.15-1.38 6.9 5.76-1.15 1.37-6.9-5.75m4.45-4.25L20 9.08l-1.44 1.07-5.36-7.21 1.44-1.07M6.59 18.41v-1.8h8.98v1.8H6.59Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stack and queue
|
||||
</span>
|
||||
|
||||
|
||||
@ -1258,7 +1264,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_6_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_6">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 5. Stack and Queue
|
||||
Chapter 5. Stack and queue
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1317,7 +1323,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
5.3 Double-ended Queue
|
||||
5.3 Double-ended queue
|
||||
</span>
|
||||
|
||||
|
||||
@ -1396,7 +1402,7 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19.3 17.89c1.32-2.1.7-4.89-1.41-6.21a4.52 4.52 0 0 0-6.21 1.41C10.36 15.2 11 18 13.09 19.3c1.47.92 3.33.92 4.8 0L21 22.39 22.39 21l-3.09-3.11m-2-.62c-.98.98-2.56.97-3.54 0-.97-.98-.97-2.56.01-3.54.97-.97 2.55-.97 3.53 0 .96.99.95 2.57-.03 3.54h.03M19 4H5a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h5.81a6.3 6.3 0 0 1-1.31-2H5v-4h4.18c.16-.71.43-1.39.82-2H5V8h6v2.81a6.3 6.3 0 0 1 2-1.31V8h6v2a6.499 6.499 0 0 1 2 2V6a2 2 0 0 0-2-2Z"/></svg>
|
||||
|
||||
<span class="md-ellipsis">
|
||||
Chapter 6. Hash Table
|
||||
Chapter 6. Hash table
|
||||
</span>
|
||||
|
||||
|
||||
@ -1412,7 +1418,7 @@
|
||||
<nav class="md-nav" data-md-level="1" aria-labelledby="__nav_7_label" aria-expanded="false">
|
||||
<label class="md-nav__title" for="__nav_7">
|
||||
<span class="md-nav__icon md-icon"></span>
|
||||
Chapter 6. Hash Table
|
||||
Chapter 6. Hash table
|
||||
</label>
|
||||
<ul class="md-nav__list" data-md-scrollfix>
|
||||
|
||||
@ -1429,7 +1435,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.1 Hash Table
|
||||
6.1 Hash table
|
||||
</span>
|
||||
|
||||
|
||||
@ -1450,7 +1456,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.2 Hash Collision
|
||||
6.2 Hash collision
|
||||
</span>
|
||||
|
||||
|
||||
@ -1471,7 +1477,7 @@
|
||||
|
||||
|
||||
<span class="md-ellipsis">
|
||||
6.3 Hash Algorithm
|
||||
6.3 Hash algorithm
|
||||
</span>
|
||||
|
||||
|
||||
@ -2046,7 +2052,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#341-ascii-character-set" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.1 ASCII Character Set
|
||||
3.4.1 ASCII character set
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2055,7 +2061,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#342-gbk-character-set" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.2 GBK Character Set
|
||||
3.4.2 GBK character set
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2064,7 +2070,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#343-unicode-character-set" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.3 Unicode Character Set
|
||||
3.4.3 Unicode character set
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2073,7 +2079,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#344-utf-8-encoding" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.4 UTF-8 Encoding
|
||||
3.4.4 UTF-8 encoding
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2082,7 +2088,7 @@
|
||||
<li class="md-nav__item">
|
||||
<a href="#345-character-encoding-in-programming-languages" class="md-nav__link">
|
||||
<span class="md-ellipsis">
|
||||
3.4.5 Character Encoding in Programming Languages
|
||||
3.4.5 Character encoding in programming languages
|
||||
</span>
|
||||
</a>
|
||||
|
||||
@ -2124,30 +2130,30 @@
|
||||
|
||||
|
||||
<!-- Page content -->
|
||||
<h1 id="34-character-encoding">3.4 Character Encoding *<a class="headerlink" href="#34-character-encoding" title="Permanent link">¶</a></h1>
|
||||
<h1 id="34-character-encoding">3.4 Character encoding *<a class="headerlink" href="#34-character-encoding" title="Permanent link">¶</a></h1>
|
||||
<p>In the computer system, all data is stored in binary form, and characters (represented by char) are no exception. To represent characters, we need to develop a "character set" that defines a one-to-one mapping between each character and binary numbers. With the character set, computers can convert binary numbers to characters by looking up the table.</p>
|
||||
<h2 id="341-ascii-character-set">3.4.1 ASCII Character Set<a class="headerlink" href="#341-ascii-character-set" title="Permanent link">¶</a></h2>
|
||||
<h2 id="341-ascii-character-set">3.4.1 ASCII character set<a class="headerlink" href="#341-ascii-character-set" title="Permanent link">¶</a></h2>
|
||||
<p>The "ASCII code" is one of the earliest character sets, officially known as the American Standard Code for Information Interchange. It uses 7 binary digits (the lower 7 bits of a byte) to represent a character, allowing for a maximum of 128 different characters. As shown in the Figure 3-6 , ASCII includes uppercase and lowercase English letters, numbers 0 ~ 9, various punctuation marks, and certain control characters (such as newline and tab).</p>
|
||||
<p><a class="glightbox" href="../character_encoding.assets/ascii_table.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="ASCII Code" class="animation-figure" src="../character_encoding.assets/ascii_table.png" /></a></p>
|
||||
<p align="center"> Figure 3-6 ASCII Code </p>
|
||||
<p><a class="glightbox" href="../character_encoding.assets/ascii_table.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="ASCII code" class="animation-figure" src="../character_encoding.assets/ascii_table.png" /></a></p>
|
||||
<p align="center"> Figure 3-6 ASCII code </p>
|
||||
|
||||
<p>However, <strong>ASCII can only represent English characters</strong>. With the globalization of computers, a character set called "EASCII" was developed to represent more languages. It expands from the 7-bit structure of ASCII to 8 bits, enabling the representation of 256 characters.</p>
|
||||
<p>Globally, various region-specific EASCII character sets have been introduced. The first 128 characters of these sets are consistent with the ASCII, while the remaining 128 characters are defined differently to accommodate the requirements of different languages.</p>
|
||||
<h2 id="342-gbk-character-set">3.4.2 GBK Character Set<a class="headerlink" href="#342-gbk-character-set" title="Permanent link">¶</a></h2>
|
||||
<h2 id="342-gbk-character-set">3.4.2 GBK character set<a class="headerlink" href="#342-gbk-character-set" title="Permanent link">¶</a></h2>
|
||||
<p>Later, it was found that <strong>EASCII still could not meet the character requirements of many languages</strong>. For instance, there are nearly a hundred thousand Chinese characters, with several thousand used regularly. In 1980, the Standardization Administration of China released the "GB2312" character set, which included 6763 Chinese characters, essentially fulfilling the computer processing needs for the Chinese language.</p>
|
||||
<p>However, GB2312 could not handle some rare and traditional characters. The "GBK" character set expands GB2312 and includes 21886 Chinese characters. In the GBK encoding scheme, ASCII characters are represented with one byte, while Chinese characters use two bytes.</p>
|
||||
<h2 id="343-unicode-character-set">3.4.3 Unicode Character Set<a class="headerlink" href="#343-unicode-character-set" title="Permanent link">¶</a></h2>
|
||||
<h2 id="343-unicode-character-set">3.4.3 Unicode character set<a class="headerlink" href="#343-unicode-character-set" title="Permanent link">¶</a></h2>
|
||||
<p>With the rapid evolution of computer technology and a plethora of character sets and encoding standards, numerous problems arose. On the one hand, these character sets generally only defined characters for specific languages and could not function properly in multilingual environments. On the other hand, the existence of multiple character set standards for the same language caused garbled text when information was exchanged between computers using different encoding standards.</p>
|
||||
<p>Researchers of that era thought: <strong>What if a comprehensive character set encompassing all global languages and symbols was developed? Wouldn't this resolve the issues associated with cross-linguistic environments and garbled text?</strong> Inspired by this idea, the extensive character set, Unicode, was born.</p>
|
||||
<p>"Unicode" is referred to as "统一码" (Unified Code) in Chinese, theoretically capable of accommodating over a million characters. It aims to incorporate characters from all over the world into a single set, providing a universal character set for processing and displaying various languages and reducing the issues of garbled text due to different encoding standards.</p>
|
||||
<p>Since its release in 1991, Unicode has continually expanded to include new languages and characters. As of September 2022, Unicode contains 149,186 characters, including characters, symbols, and even emojis from various languages. In the vast Unicode character set, commonly used characters occupy 2 bytes, while some rare characters may occupy 3 or even 4 bytes.</p>
|
||||
<p>Unicode is a universal character set that assigns a number (called a "code point") to each character, <strong>but it does not specify how these character code points should be stored in a computer system</strong>. One might ask: How does a system interpret Unicode code points of varying lengths within a text? For example, given a 2-byte code, how does the system determine if it represents a single 2-byte character or two 1-byte characters?</p>
|
||||
<p>A straightforward solution to this problem is to store all characters as equal-length encodings. As shown in the Figure 3-7 , each character in "Hello" occupies 1 byte, while each character in "算法" (algorithm) occupies 2 bytes. We could encode all characters in "Hello 算法" as 2 bytes by padding the higher bits with zeros. This method would enable the system to interpret a character every 2 bytes, recovering the content of the phrase.</p>
|
||||
<p><a class="glightbox" href="../character_encoding.assets/unicode_hello_algo.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Unicode Encoding Example" class="animation-figure" src="../character_encoding.assets/unicode_hello_algo.png" /></a></p>
|
||||
<p align="center"> Figure 3-7 Unicode Encoding Example </p>
|
||||
<p><a class="glightbox" href="../character_encoding.assets/unicode_hello_algo.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="Unicode encoding example" class="animation-figure" src="../character_encoding.assets/unicode_hello_algo.png" /></a></p>
|
||||
<p align="center"> Figure 3-7 Unicode encoding example </p>
|
||||
|
||||
<p>However, as ASCII has shown us, encoding English only requires 1 byte. Using the above approach would double the space occupied by English text compared to ASCII encoding, which is a waste of memory space. Therefore, a more efficient Unicode encoding method is needed.</p>
|
||||
<h2 id="344-utf-8-encoding">3.4.4 UTF-8 Encoding<a class="headerlink" href="#344-utf-8-encoding" title="Permanent link">¶</a></h2>
|
||||
<h2 id="344-utf-8-encoding">3.4.4 UTF-8 encoding<a class="headerlink" href="#344-utf-8-encoding" title="Permanent link">¶</a></h2>
|
||||
<p>Currently, UTF-8 has become the most widely used Unicode encoding method internationally. <strong>It is a variable-length encoding</strong>, using 1 to 4 bytes to represent a character, depending on the complexity of the character. ASCII characters need only 1 byte, Latin and Greek letters require 2 bytes, commonly used Chinese characters need 3 bytes, and some other rare characters need 4 bytes.</p>
|
||||
<p>The encoding rules for UTF-8 are not complex and can be divided into two cases:</p>
|
||||
<ul>
|
||||
@ -2157,22 +2163,22 @@
|
||||
<p>The Figure 3-8 shows the UTF-8 encoding for "Hello算法". It can be observed that since the highest <span class="arithmatex">\(n\)</span> bits are set to <span class="arithmatex">\(1\)</span>, the system can determine the length of the character as <span class="arithmatex">\(n\)</span> by counting the number of highest bits set to <span class="arithmatex">\(1\)</span>.</p>
|
||||
<p>But why set the highest 2 bits of the remaining bytes to <span class="arithmatex">\(10\)</span>? Actually, this <span class="arithmatex">\(10\)</span> serves as a kind of checksum. If the system starts parsing text from an incorrect byte, the <span class="arithmatex">\(10\)</span> at the beginning of the byte can help the system quickly detect anomalies.</p>
|
||||
<p>The reason for using <span class="arithmatex">\(10\)</span> as a checksum is that, under UTF-8 encoding rules, it's impossible for the highest two bits of a character to be <span class="arithmatex">\(10\)</span>. This can be proven by contradiction: If the highest two bits of a character are <span class="arithmatex">\(10\)</span>, it indicates that the character's length is <span class="arithmatex">\(1\)</span>, corresponding to ASCII. However, the highest bit of an ASCII character should be <span class="arithmatex">\(0\)</span>, which contradicts the assumption.</p>
|
||||
<p><a class="glightbox" href="../character_encoding.assets/utf-8_hello_algo.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="UTF-8 Encoding Example" class="animation-figure" src="../character_encoding.assets/utf-8_hello_algo.png" /></a></p>
|
||||
<p align="center"> Figure 3-8 UTF-8 Encoding Example </p>
|
||||
<p><a class="glightbox" href="../character_encoding.assets/utf-8_hello_algo.png" data-type="image" data-width="100%" data-height="auto" data-desc-position="bottom"><img alt="UTF-8 encoding example" class="animation-figure" src="../character_encoding.assets/utf-8_hello_algo.png" /></a></p>
|
||||
<p align="center"> Figure 3-8 UTF-8 encoding example </p>
|
||||
|
||||
<p>Apart from UTF-8, other common encoding methods include:</p>
|
||||
<ul>
|
||||
<li><strong>UTF-16 Encoding</strong>: Uses 2 or 4 bytes to represent a character. All ASCII characters and commonly used non-English characters are represented with 2 bytes; a few characters require 4 bytes. For 2-byte characters, the UTF-16 encoding equals the Unicode code point.</li>
|
||||
<li><strong>UTF-32 Encoding</strong>: Every character uses 4 bytes. This means UTF-32 occupies more space than UTF-8 and UTF-16, especially for texts with a high proportion of ASCII characters.</li>
|
||||
<li><strong>UTF-16 encoding</strong>: Uses 2 or 4 bytes to represent a character. All ASCII characters and commonly used non-English characters are represented with 2 bytes; a few characters require 4 bytes. For 2-byte characters, the UTF-16 encoding equals the Unicode code point.</li>
|
||||
<li><strong>UTF-32 encoding</strong>: Every character uses 4 bytes. This means UTF-32 occupies more space than UTF-8 and UTF-16, especially for texts with a high proportion of ASCII characters.</li>
|
||||
</ul>
|
||||
<p>From the perspective of storage space, using UTF-8 to represent English characters is very efficient because it only requires 1 byte; using UTF-16 to encode some non-English characters (such as Chinese) can be more efficient because it only requires 2 bytes, while UTF-8 might need 3 bytes.</p>
|
||||
<p>From a compatibility perspective, UTF-8 is the most versatile, with many tools and libraries supporting UTF-8 as a priority.</p>
|
||||
<h2 id="345-character-encoding-in-programming-languages">3.4.5 Character Encoding in Programming Languages<a class="headerlink" href="#345-character-encoding-in-programming-languages" title="Permanent link">¶</a></h2>
|
||||
<h2 id="345-character-encoding-in-programming-languages">3.4.5 Character encoding in programming languages<a class="headerlink" href="#345-character-encoding-in-programming-languages" title="Permanent link">¶</a></h2>
|
||||
<p>Historically, many programming languages utilized fixed-length encodings such as UTF-16 or UTF-32 for processing strings during program execution. This allows strings to be handled as arrays, offering several advantages:</p>
|
||||
<ul>
|
||||
<li><strong>Random Access</strong>: Strings encoded in UTF-16 can be accessed randomly with ease. For UTF-8, which is a variable-length encoding, locating the <span class="arithmatex">\(i^{th}\)</span> character requires traversing the string from the start to the <span class="arithmatex">\(i^{th}\)</span> position, taking <span class="arithmatex">\(O(n)\)</span> time.</li>
|
||||
<li><strong>Character Counting</strong>: Similar to random access, counting the number of characters in a UTF-16 encoded string is an <span class="arithmatex">\(O(1)\)</span> operation. However, counting characters in a UTF-8 encoded string requires traversing the entire string.</li>
|
||||
<li><strong>String Operations</strong>: Many string operations like splitting, concatenating, inserting, and deleting are easier on UTF-16 encoded strings. These operations generally require additional computation on UTF-8 encoded strings to ensure the validity of the UTF-8 encoding.</li>
|
||||
<li><strong>Random access</strong>: Strings encoded in UTF-16 can be accessed randomly with ease. For UTF-8, which is a variable-length encoding, locating the <span class="arithmatex">\(i^{th}\)</span> character requires traversing the string from the start to the <span class="arithmatex">\(i^{th}\)</span> position, taking <span class="arithmatex">\(O(n)\)</span> time.</li>
|
||||
<li><strong>Character counting</strong>: Similar to random access, counting the number of characters in a UTF-16 encoded string is an <span class="arithmatex">\(O(1)\)</span> operation. However, counting characters in a UTF-8 encoded string requires traversing the entire string.</li>
|
||||
<li><strong>String operations</strong>: Many string operations like splitting, concatenating, inserting, and deleting are easier on UTF-16 encoded strings. These operations generally require additional computation on UTF-8 encoded strings to ensure the validity of the UTF-8 encoding.</li>
|
||||
</ul>
|
||||
<p>The design of character encoding schemes in programming languages is an interesting topic involving various factors:</p>
|
||||
<ul>
|
||||
@ -2210,7 +2216,7 @@ aria-label="Footer"
|
||||
<a
|
||||
href="../number_encoding/"
|
||||
class="md-footer__link md-footer__link--prev"
|
||||
aria-label="Previous: 3.3 Number Encoding *"
|
||||
aria-label="Previous: 3.3 Number encoding *"
|
||||
rel="prev"
|
||||
>
|
||||
<div class="md-footer__button md-icon">
|
||||
@ -2222,7 +2228,7 @@ aria-label="Footer"
|
||||
Previous
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
3.3 Number Encoding *
|
||||
3.3 Number encoding *
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
@ -2335,7 +2341,7 @@ aria-label="Footer"
|
||||
<nav class="md-footer__inner md-grid" aria-label="Footer" >
|
||||
|
||||
|
||||
<a href="../number_encoding/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.3 Number Encoding *">
|
||||
<a href="../number_encoding/" class="md-footer__link md-footer__link--prev" aria-label="Previous: 3.3 Number encoding *">
|
||||
<div class="md-footer__button md-icon">
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
|
||||
@ -2345,7 +2351,7 @@ aria-label="Footer"
|
||||
Previous
|
||||
</span>
|
||||
<div class="md-ellipsis">
|
||||
3.3 Number Encoding *
|
||||
3.3 Number encoding *
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
|
Reference in New Issue
Block a user