Unicode To UTF8 及编码过程实时解析
快速尝试:
hello, world
αβγδ
道可道非常道
𠮷
🍉🍇🍑🍓🥝

转化过程解析

Unicode
Binary
UTF8
1
Prefix Code
1
Unicode二进制组
1
Unicode二进制组(便于区分)

Unicode To UTF8 编码与解码

UTF-8 小解

UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,也是一种前缀码。见 UTF-8

它可以用一至四个字节对 Unicode 字符集中的所有有效编码点进行编码,属于Unicode标准的一部分,由于较小值的编码点一般使用频率较高,直接使用Unicode编码效率低下,大量浪费内存空间。

UTF-8就是为了解决向后兼容ASCII码而设计,Unicode中前128个字符,使用与ASCII码相同的二进制值的单个字节进行编码,而且字面与ASCII码的字面一一对应,这使得原来处理ASCII字符的软件无须或只须做少部分修改,即可继续使用。

UTF8 Code Point Prefix

| Number of bytes | Bits for code point | First code point | Last code point | Byte 1 | Byte 2 | Byte 3 | Byte 4 | |-----------------|---------------------|------------------|-----------------|----------|----------|----------|----------| | 1 | 7 | U+0000 | U+007F | 0xxxxxxx | | | | | 2 | 11 | U+0080 | U+07FF | 110xxxxx | 10xxxxxx | | | | 3 | 16 | U+0800 | U+FFFF | 1110xxxx | 10xxxxxx | 10xxxxxx | | | 4 | 21 | U+10000 | U+10FFFF | 11110xxx | 10xxxxxx | 10xxxxxx | 10xxxxxx |

其中:

转化过程

  1. 将数据转化为 unicode 的 codePoint,在 JS 中可借助于 str.codePointAt(0)
  2. 将转化后的 codePoint 根据前缀表确定其所占用的字节数,如 codePoint 为 U+0801,范围在 U+0800 与 U+FFFF 之间,占用三个字节
  3. 根据字节数,确定其前缀,并将 codePoint 转化为二进制从右填充
  4. 将每八个二进制计算得出其 UTF-8 编码

如,

  1. 根据 '山'.codePointAt().toString(16) 得出其十六进制为 0x9053
  2. 根据 0x9053,可知其占用三个字节,确定前缀 1110xxxx 10xxxxxx 10xxxxxx 并根据自身大小进行填充,得到 11101001 10000001 10010011
  3. 根据填充所得的码,转化为三个 UTF-8,得到 E9 81 93

API

JS

在 Javascript 中,可借助于 TextEcoder API 进行转化

export function stringToUint8 (s: string): Uint8Array {
  const enc = new TextEncoder()
  return enc.encode(s)
}

export function uint8ToString (uint8: Uint8Array): string {
  const enc = new TextDecoder()
  return enc.decode(uint8)
}

Python

> '山月'.encode()
b'\xe5\xb1\xb1\xe6\x9c\x88'

> b'\xe5\xb1\xb1\xe6\x9c\x88'.decode()
山月