猿问

python中十六进制流中的格式字符串

我使用 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]


阿晨1998
浏览 234回答 1
1回答

慕森卡

您可以使用以下bytearray.fromhex()方法:>>> bytearray.fromhex('123456')bytearray(b'\x124V') # which is the same as b'\x12\x34\x56'
随时随地看视频慕课网APP

相关分类

Python
我要回答