python3 字符串 hex 相互转换 代替python2 decode(‘hex’) |
您所在的位置:网站首页 › Hex数据类型 › python3 字符串 hex 相互转换 代替python2 decode(‘hex’) |
1.python2.7.x
hex字符串和bytes之间的转换是这样的: >>> a = 'aabbccddeeff' >>> a_bytes = a.decode('hex') >>> print(a_bytes) b'\xaa\xbb\xcc\xdd\xee\xff' >>> aa = a_bytes.encode('hex') >>> print(aa) aabbccddeeff >>>在python 3环境上,因为string和bytes的实现发生了重大的变化,这个转换也不能再用encode/decode完成了。 2.python3.5之前这个转换的其中一种方式是这样的: >>> a = 'aabbccddeeff' >>> a_bytes = bytes.fromhex(a) >>> print(a_bytes) b'\xaa\xbb\xcc\xdd\xee\xff' >>> aa = ''.join(['%02x' % b for b in a_bytes]) >>> print(aa) aabbccddeeff >>> 3.python 3.5后就可以像下面这么干了: >>> a = 'aabbccddeeff' >>> a_bytes = bytes.fromhex(a) >>> print(a_bytes) b'\xaa\xbb\xcc\xdd\xee\xff' >>> aa = a_bytes.hex() >>> print(aa) aabbccddeeff >>>
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |