python bytes的hex和fromhex函数

python bytes的hex和fromhex函数

bytes对象的hex函数,用来将bytes对象的值转换成hexstr;
而fromhex函数,用来将hexstr导入bytes对象,相当于用hexstr来创建bytes对象。
bytes对象的这两个函数,有的时候非常好用!

>>> bytes([0,1,2,3,4,5]).hex()
'000102030405'
>>> bytes.fromhex('000102030405')
b'\x00\x01\x02\x03\x04\x05'
>>> b'abcde'.hex()
'6162636465'
>>> a = bytes.fromhex('6162636465')
>>> a
b'abcde'

bytes对象的hex函数可以用来对bytes对象的内容进行显示,而fromhex可以用来对输入进行直接bytes化。

猜你喜欢

转载自blog.csdn.net/wowocpp/article/details/115347344