1. 程式人生 > >python3編碼的轉換

python3編碼的轉換

例子

中國 -> utf-8編碼成的bytes -> 16進位制字串

>>> word = "中國"
>>> data = word.encode("utf-8")
>>> data
b'\xe4\xb8\xad\xe5\x9b\xbd'
>>> import binascii
>>> binascii.b2a_hex(data)
b'e4b8ade59bbd'

16進位制字串 -> utf-8編碼成的bytes -> 中國

>>> str = "e4b8ade59bbd"
>>> import binascii >>> data = binascii.a2b_hex(str) >>> data.decode("utf-8") '中國'