[英]python converting hexadecimal binary to string
我正在使用python3.5
,我希望将以十六进制字节( b'\\x00'
, b'\\x01'
等)获得的输出写入python字符串中,并带有\\x00 -> 0
和\\x01 -> 1
,我有这个感觉它可以很容易地以Python的方式完成,但是半小时的谷歌搜索仍然使我认为最简单的方法是手工制作带有映射的字典(我实际上只需要0到7)。
Input Intended output
b'\x00' 0 or '0'
b'\x01' 1 or '1'
等等
不确定是否要此结果,但是请尝试
output = [str(ord(x)) for x in output]
字节字符串自动是数字列表。
input_bytes = b"\x00\x01"
output_numbers = list(input_bytes)
您只是在寻找这样的东西吗?
for x in range(0,8):
(x).to_bytes(1, byteorder='big')
输出为:
b'\x00'
b'\x01'
b'\x02'
b'\x03'
b'\x04'
b'\x05'
b'\x06'
b'\x07'
或相反:
byteslist = [b'\x00',
b'\x01',
b'\x02',
b'\x03',
b'\x04',
b'\x05',
b'\x06',
b'\x07']
for x in byteslist:
int.from_bytes(x,byteorder='big')
输出:
0
1
2
3
4
5
6
7
如果需要将b"\\x0F"
转换为F
使用
print( hex(ord(b'\x0F'))[2:] )
或使用format()
print( format(ord(b'\x0F'), 'X') ) # '02X' gives string '0F'
print( '{:X}'.format(ord(b'\x0F')) ) # '{:02X}' gives string '0F'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.