为什么说一个中文占三个字节
<p>缘由</p><blockquote>
<p>在学习java基础时<br>
<s>对于s2,一个中文占用3个字节**,21845个正好占用65535个字节,而且字符串长度是21845,长度和存储也都没超过限制,所以可以编译通过</s><br>
后来发现这句话是错的, java中char的存储是 LATIN-1(1字节) OR UTF-16(2字节)<br>
但是引出了我对 UTF-8 的思考<br>
为啥是三个字节, 不是两个字节吗</p>
</blockquote>
<ul>
<li>java的默认编码格式</li>
</ul>
<p>那我们看看 UTF-8的wiki</p>
<ul>
<li>Unicode</li>
</ul>
<p>故名思意, 为每个字符(任何语言)提供一个独特编码<br>
以满足跨语言、跨平台的文本信息转换</p>
<table>
<thead>
<tr>
<th>码点的位数</th>
<th>表示的unicode范围</th>
<th>字节序列</th>
<th>Byte 1</th>
<th>Byte 2</th>
<th>Byte 3</th>
<th>Byte4</th>
</tr>
</thead>
<tbody>
<tr>
<td>7</td>
<td>0~127</td>
<td>1</td>
<td><code>0xxxxxxx</code></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>11</td>
<td>128~2047</td>
<td>2</td>
<td><code>110xxxxx</code></td>
<td><code>10xxxxxx</code></td>
<td></td>
<td></td>
</tr>
<tr>
<td>16</td>
<td>2048~65535</td>
<td>3</td>
<td><code>1110xxxx</code></td>
<td><code>10xxxxxx</code></td>
<td><code>10xxxxxx</code></td>
<td></td>
</tr>
<tr>
<td>21</td>
<td>>65535</td>
<td>4</td>
<td><code>11110xxx</code></td>
<td><code>10xxxxxx</code></td>
<td><code>10xxxxxx</code></td>
<td><code>10xxxxxx</code></td>
</tr>
</tbody>
</table>
<p>比如’中’的unicode为<strong><code>4E2D</code></strong>4^(16∗16∗16) = 16384 >2047</p>
<p>显然是用3位UTF-8存储</p><br><br>
来源:https://www.cnblogs.com/many-bucket/p/18927113
頁:
[1]