This commit is contained in:
krahets
2026-04-04 23:10:58 +08:00
parent 5b59dbded2
commit fec71ace08
168 changed files with 308 additions and 963 deletions

View File

@@ -1149,7 +1149,7 @@
<a href="#341-ascii-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.1 &nbsp; Ascii Character Set
3.4.1 &nbsp; ASCII Character Set
</span>
</a>
@@ -1160,7 +1160,7 @@
<a href="#342-gbk-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.2 &nbsp; Gbk Character Set
3.4.2 &nbsp; GBK Character Set
</span>
</a>
@@ -1182,7 +1182,7 @@
<a href="#344-utf-8-encoding" class="md-nav__link">
<span class="md-ellipsis">
3.4.4 &nbsp; Utf-8 Encoding
3.4.4 &nbsp; UTF-8 Encoding
</span>
</a>
@@ -1698,7 +1698,7 @@
<span class="md-ellipsis">
Chapter 6. Hash Table
Chapter 6. Hashing
@@ -1720,7 +1720,7 @@
<span class="md-nav__icon md-icon"></span>
Chapter 6. Hash Table
Chapter 6. Hashing
</label>
@@ -4334,7 +4334,7 @@
<a href="#341-ascii-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.1 &nbsp; Ascii Character Set
3.4.1 &nbsp; ASCII Character Set
</span>
</a>
@@ -4345,7 +4345,7 @@
<a href="#342-gbk-character-set" class="md-nav__link">
<span class="md-ellipsis">
3.4.2 &nbsp; Gbk Character Set
3.4.2 &nbsp; GBK Character Set
</span>
</a>
@@ -4367,7 +4367,7 @@
<a href="#344-utf-8-encoding" class="md-nav__link">
<span class="md-ellipsis">
3.4.4 &nbsp; Utf-8 Encoding
3.4.4 &nbsp; UTF-8 Encoding
</span>
</a>
@@ -4424,20 +4424,20 @@
<!-- Page content -->
<h1 id="34-character-encoding">3.4 &nbsp; Character Encoding *<a class="headerlink" href="#34-character-encoding" title="Permanent link">&para;</a></h1>
<p>In computers, all data is stored in binary form, and character <code>char</code> is no exception. To represent characters, we need to establish a "character set" that defines a one-to-one correspondence between each character and binary numbers. With a character set, computers can convert binary numbers to characters by looking up the table.</p>
<h2 id="341-ascii-character-set">3.4.1 &nbsp; Ascii Character Set<a class="headerlink" href="#341-ascii-character-set" title="Permanent link">&para;</a></h2>
<h2 id="341-ascii-character-set">3.4.1 &nbsp; ASCII Character Set<a class="headerlink" href="#341-ascii-character-set" title="Permanent link">&para;</a></h2>
<p><u>ASCII code</u> is the earliest character set, with the full name American Standard Code for Information Interchange. It uses 7 binary bits (the lower 7 bits of one byte) to represent a character, and can represent a maximum of 128 different characters. As shown in Figure 3-6, ASCII code includes uppercase and lowercase English letters, numbers 0 ~ 9, some punctuation marks, and some control characters (such as newline and tab).</p>
<p><img alt="ASCII code" class="animation-figure" src="../character_encoding.assets/ascii_table.png" /></p>
<p align="center"> Figure 3-6 &nbsp; ASCII code </p>
<p>However, <strong>ASCII code can only represent English</strong>. With the globalization of computers, a character set called <u>EASCII</u> that can represent more languages emerged. It expands from the 7-bit basis of ASCII to 8 bits, and can represent 256 different characters.</p>
<p>Worldwide, a batch of EASCII character sets suitable for different regions have appeared successively. The first 128 characters of these character sets are unified as ASCII code, and the last 128 characters are defined differently to adapt to the needs of different languages.</p>
<h2 id="342-gbk-character-set">3.4.2 &nbsp; Gbk Character Set<a class="headerlink" href="#342-gbk-character-set" title="Permanent link">&para;</a></h2>
<h2 id="342-gbk-character-set">3.4.2 &nbsp; GBK Character Set<a class="headerlink" href="#342-gbk-character-set" title="Permanent link">&para;</a></h2>
<p>Later, people found that <strong>EASCII still could not provide enough characters for many languages</strong>. For example, there are nearly one hundred thousand Chinese characters, and several thousand are used in everyday life. In 1980, the China National Standardization Administration released the <u>GB2312</u> character set, which included 6,763 Chinese characters, basically meeting the needs of computer processing for Chinese.</p>
<p>However, GB2312 cannot handle some rare characters and traditional Chinese characters. The <u>GBK</u> character set is an extension based on GB2312, which includes a total of 21,886 Chinese characters. In the GBK encoding scheme, ASCII characters are represented using one byte, and Chinese characters are represented using two bytes.</p>
<h2 id="343-unicode-character-set">3.4.3 &nbsp; Unicode Character Set<a class="headerlink" href="#343-unicode-character-set" title="Permanent link">&para;</a></h2>
<p>With the vigorous development of computer technology, character sets and encoding standards flourished, which brought many problems. On the one hand, these character sets generally only define characters for specific languages and cannot work normally in multilingual environments. On the other hand, multiple character set standards exist for the same language, and if two computers use different encoding standards, garbled characters will appear during information transmission.</p>
<p>Researchers of that era thought: <strong>If a sufficiently complete character set were released to include all languages and symbols in the world, wouldn't that solve problems in cross-language environments and eliminate garbled text</strong>? Driven by this idea, a large and comprehensive character set, Unicode, was born.</p>
<p><u>Unicode</u> is called "统一码" (Unified Code) in Chinese and can theoretically accommodate over one million characters. It is committed to including characters from around the world into a unified character set, providing a universal character set to handle and display various language texts, reducing garbled character problems caused by different encoding standards.</p>
<p><u>Unicode</u>, or Unified Code, can theoretically accommodate over one million characters. It is committed to including characters from around the world into a unified character set, providing a universal character set to handle and display various language texts, reducing garbled character problems caused by different encoding standards.</p>
<p>Since its release in 1991, Unicode has continuously expanded to include new languages and characters. As of September 2022, Unicode has included 149,186 characters, including characters, symbols, and even emojis from various languages. In practical storage and encoding schemes for this vast character set, commonly used characters often occupy 2 bytes, while some rare characters occupy 3 bytes or even 4 bytes.</p>
<p>Unicode is a universal character set that essentially assigns a number (called a "code point") to each character, <strong>but it does not specify how to store these character code points in computers</strong>. We can't help but ask: when Unicode code points of multiple lengths appear simultaneously in a text, how does the system parse the characters? For example, given an encoding with a length of 2 bytes, how does the system determine whether it is one 2-byte character or two 1-byte characters?</p>
<p>For the above problem, <strong>a straightforward solution is to store all characters as equal-length encodings</strong>. As shown in Figure 3-7, each character in "Hello" occupies 1 byte, and each character in "算法" (algorithm) occupies 2 bytes. We can encode all characters in "Hello 算法" as 2 bytes in length by padding the high bits with 0. In this way, the system can parse one character every 2 bytes and restore the content of this phrase.</p>
@@ -4445,14 +4445,14 @@
<p align="center"> Figure 3-7 &nbsp; Unicode encoding example </p>
<p>However, ASCII code has already proven to us that encoding English only requires 1 byte. If the above scheme is adopted, the size of English text will be twice that under ASCII encoding, which is very wasteful of memory space. Therefore, we need a more efficient Unicode encoding method.</p>
<h2 id="344-utf-8-encoding">3.4.4 &nbsp; Utf-8 Encoding<a class="headerlink" href="#344-utf-8-encoding" title="Permanent link">&para;</a></h2>
<h2 id="344-utf-8-encoding">3.4.4 &nbsp; UTF-8 Encoding<a class="headerlink" href="#344-utf-8-encoding" title="Permanent link">&para;</a></h2>
<p>Currently, UTF-8 has become the most widely used Unicode encoding method internationally. <strong>It is a variable-length encoding</strong> that uses 1 to 4 bytes to represent a character, depending on the complexity of the character. ASCII characters only require 1 byte, Latin and Greek letters require 2 bytes, commonly used Chinese characters require 3 bytes, and some other rare characters require 4 bytes.</p>
<p>The encoding rules of UTF-8 are not complicated and can be divided into the following two cases.</p>
<ul>
<li>For 1-byte characters, set the highest bit to <span class="arithmatex">\(0\)</span>, and set the remaining 7 bits to the Unicode code point. It is worth noting that ASCII characters occupy the first 128 code points in the Unicode character set. That is to say, <strong>UTF-8 encoding is backward compatible with ASCII code</strong>. This means we can use UTF-8 to parse very old ASCII code text.</li>
<li>For characters with a length of <span class="arithmatex">\(n\)</span> bytes (where <span class="arithmatex">\(n &gt; 1\)</span>), set the highest <span class="arithmatex">\(n\)</span> bits of the first byte to <span class="arithmatex">\(1\)</span>, and set the <span class="arithmatex">\((n + 1)\)</span>-th bit to <span class="arithmatex">\(0\)</span>; starting from the second byte, set the highest 2 bits of each byte to <span class="arithmatex">\(10\)</span>; use all remaining bits to fill in the Unicode code point of the character.</li>
</ul>
<p>Figure 3-8 shows the UTF-8 encoding corresponding to "Hello算法". It can be observed that since the highest <span class="arithmatex">\(n\)</span> bits are all set to <span class="arithmatex">\(1\)</span>, the system can determine that the character length is <span class="arithmatex">\(n\)</span> by counting the leading <span class="arithmatex">\(1\)</span> bits.</p>
<p>Figure 3-8 shows the UTF-8 encoding corresponding to "Hello 算法". It can be observed that since the highest <span class="arithmatex">\(n\)</span> bits are all set to <span class="arithmatex">\(1\)</span>, the system can determine that the character length is <span class="arithmatex">\(n\)</span> by counting the leading <span class="arithmatex">\(1\)</span> bits.</p>
<p>But why set the highest 2 bits of all other bytes to <span class="arithmatex">\(10\)</span>? In fact, this <span class="arithmatex">\(10\)</span> can serve as a check symbol. Assuming 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 determine an anomaly.</p>
<p>The reason for using <span class="arithmatex">\(10\)</span> as a check symbol is that under UTF-8 encoding rules, it is impossible for a character's highest two bits to be <span class="arithmatex">\(10\)</span>. This conclusion can be proven by contradiction: assuming the highest two bits of a character are <span class="arithmatex">\(10\)</span>, it means the length of the character is <span class="arithmatex">\(1\)</span>, corresponding to ASCII code. However, the highest bit of ASCII code should be <span class="arithmatex">\(0\)</span>, which contradicts the assumption.</p>
<p><img alt="UTF-8 encoding example" class="animation-figure" src="../character_encoding.assets/utf-8_hello_algo.png" /></p>