1

如果您有一串位,如果0s 和1s 的字符串采用 utf-8 编码,您如何将其转换为正确的代码点。

例如:

accented_a_part1 = "100001"
accented_a_part2 = "00011"

accented_a_int = int(accented_a_part2 + accented_a_part1, 2)
print(accented_a_int),          # => 225
print(unichr(accented_a_int))   # => á    http://www.unicode.org/charts/PDF/U0080.pdf

accented_a_in_utf8 = "110" + accented_a_part2 + "10" + accented_a_part1
accented_a_in_utf8_as_raw_int = int(accented_a_in_utf8, 2)
print(accented_a_in_utf8_as_raw_int),        # => 50081 (not the codepoint you want)
print(unichr(accented_a_in_utf8_as_raw_int)) # => 쎡    (and therefore not the character you want)

UTF-8 规范

4

1 回答 1

2

要将文字十六进制转换为 Unicode:

>>> h = '16 03 01 00 e3 01'
>>> h.replace(' ','').decode('hex')
'\x16\x03\x01\x00\xe3\x01'
>>> h.replace(' ','').decode('hex').decode('utf8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe3 in position 4: unexpected end of data
>>>

如果十六进制是实际的 utf8,你最终会得到一个 Unicode 字符串:

>>> h = 'c3 a1'
>>> u = h.replace(' ','').decode('hex').decode('utf8')
>>> u
u'\xe1'
>>> print u
á
于 2013-03-08T13:36:32.300 回答