我使用 Windows 7,python 2.7。我想将字符串格式化为十六进制。例如,如果string = '123456 我必须转换为\x12\x34\x56我试过这个但失败了:string = '123456'b'\x%s\x%s\x%s' % string[0:2], string[2:4], string[4:6]
1 回答

慕森卡
TA贡献1806条经验 获得超8个赞
您可以使用以下bytearray.fromhex()方法:
>>> bytearray.fromhex('123456')
bytearray(b'\x124V') # which is the same as b'\x12\x34\x56'
添加回答
举报
0/150
提交
取消