【问题标题】:UTF-8 hex to unicode code point (only math)UTF-8 十六进制到 unicode 代码点(仅数学)
【发布时间】:2021-10-20 09:44:11
【问题描述】:

让我们来看看这张表,其中包含 Unicode 和 UTF-8 中的字符和 HEX 编码。
有谁知道如何仅使用数学运算将 UTF-8 十六进制转换为 Unicode 代码点?
例如。让我们占据第一行。给定227,129130怎么得到12354
有什么简单的方法可以只使用数学运算吗?

Unicode code point UTF-8 Char
30 42 (12354) e3 (227) 81 (129) 82 (130)
30 44 (12356) e3 (227) 81 (129) 84 (132)
30 46 (12358) e3 (227) 81 (129) 86 (134)

* 来源:https://www.utf8-chartable.de/unicode-utf8-table.pl?start=12288&unicodeinhtml=hex

【问题讨论】:

  • 按位与(&)、或(|)和左移(((227 & 0x0f) << 12) | ((129 & 0x3f) << 6) | (130 & 0x3f) -> 12354。请注意,这是 3 字节 UTF-8 序列的固定数学运算,不检查无效序列。

标签: math unicode utf-8 hex


【解决方案1】:

This video 是完美的来源(从 6:15 开始观看),但这里是它的摘要和 golang 代码示例。我用字母标记取自 UTF-8 字节的位,希望它有意义。当您了解逻辑后,很容易应用位运算符):

Bytes Char UTF-8 bytes Unicode code point Explanation
1-byte (ASCII) E 1. 0xxx xxxx
0100 0101 or 0x45
1. 0xxx xxxx
0100 0101 or U+0045
no conversion needed, the same value in UTF-8 and unicode code point
2-byte Ê 1. 110x xxxx
2. 10yy yyyy
1100 0011 1000 1010 or 0xC38A
0xxx xxyy yyyy
0000 1100 1010 or U+00CA
1. First 5 bits of the 1st byte
2. First 6 bits of the 2nd byte
3-byte 1. 1110 xxxx
2. 10yy yyyy
3. 10zz zzzz
1110 0011 1000 0001 1000 0010 or 0xE38182
xxxx yyyy yyzz zzzz
0011 0000 0100 0010 or U+3042
1. First 4 bits of the 1st byte
2. First 6 bits of the 2nd byte
3. First 6 bits of the 3rd byte
4-byte ? 1. 1111 0xxx
2. 10yy yyyy
3. 10zz zzzz
4. 10ww wwww
1111 0000 1001 0000 1000 0100 1001 1111 or 0xF090_849F
000x xxyy yyyy zzzz zzww wwww
0000 0001 0000 0001 0001 1111 or U+1011F
1. First 3 bits of the 1st byte
2. First 6 bits of the 2nd byte
3. First 6 bits of the 3rd byte
4. First 6 bits of the 4th byte

2 字节 UTF-8

func get(byte1 byte, byte2 byte) {
    int1 := uint16(byte1 & 0b_0001_1111) << 6
    int2 := uint16(byte2 & 0b_0011_111)
    return rune(int1 + int2)
}

3 字节 UTF-8

func get(byte1 byte, byte2 byte, byte3 byte) {
    int1 := uint16(byte1 & 0b_0000_1111) << 12
    int2 := uint16(byte2 & 0b_0011_111) << 6
    int3 := uint16(byte3 & 0b_0011_111)
    return rune(int1 + int2 + int3)
}

4 字节 UTF-8

func get(byte1 byte, byte2 byte, byte3 byt3, byte4 byte) {
    int1 := uint(byte1 & 0b_0000_1111) << 18
    int2 := uint(byte2 & 0b_0011_111) << 12
    int3 := uint(byte3 & 0b_0011_111) << 6
    int4 := uint(byte4 & 0b_0011_111)
    return rune(int1 + int2 + int3 + int4)
}

【讨论】:

    猜你喜欢
    • 2015-12-06
    • 2020-02-06
    • 2011-06-10
    • 1970-01-01
    • 2014-08-07
    • 2013-04-12
    • 1970-01-01
    • 2020-05-27
    相关资源
    最近更新 更多