字符串与十六进制的转换问题
python3.6实现:
字符串 -- 》十六进制
import binascii
data = "中文测试用例"
data = binascii.b2a_hex(data.encode("utf8"))
datas = data.decode(encoding='utf-8')
print(type(datas))
print(datas)
十六进制 -- 》字符串
import binascii
text = binascii.a2b_hex('e4b8ade69687e6b58be8af95e794a8e4be8b')
text = text.decode(encoding='utf-8')
print(type(text))
print(text)
python2.7实现:
字符串 -- 》十六进制
# coding:utf-8
import binascii
text = binascii.b2a_hex("中文测试用例")
print type(text)
print text
如果出现了这样的保存只需要添加:
'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
import sys
reload(sys)
sys.setdefaultencoding('utf8')
十六进制 -- 》字符串
# coding:utf-8
import binascii
text = binascii.a2b_hex("e4b8ade69687e6b58be8af95e794a8e4be8b")
print text
网友评论