python 将十六进制二进制转换为字符串

fbe*_*nce 8 python binary hex python-3.x

我正在使用python3.5并且我希望将我得到的十六进制字节(b'\x00'b'\x01')输出写入Python字符串,并且\x00 -> 0\x01 -> 1有这种感觉它可以轻松地以非常Pythonic的方式完成,但是半个小时的谷歌搜索仍然让我思考最简单的方法是手工制作一本带有映射的字典(我实际上只需要从 0 到 7)。

Input    Intended output
b'\x00'  0 or '0'
b'\x01'  1 or '1'
Run Code Online (Sandbox Code Playgroud)

ETC。

Dan*_*iel 7

字节字符串自动是数字列表。

input_bytes = b"\x00\x01"
output_numbers = list(input_bytes)
Run Code Online (Sandbox Code Playgroud)


小智 7

您只是在寻找这样的东西吗?

for x in range(0,8):
    (x).to_bytes(1, byteorder='big')
Run Code Online (Sandbox Code Playgroud)

输出是:

b'\x00'
b'\x01'
b'\x02'
b'\x03'
b'\x04'
b'\x05'
b'\x06'
b'\x07'
Run Code Online (Sandbox Code Playgroud)

或者相反:

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')
Run Code Online (Sandbox Code Playgroud)

输出:

0
1
2
3
4
5
6
7
Run Code Online (Sandbox Code Playgroud)


Jar*_*otr 5

Not sure if you want this result, but try it

output = [str(ord(x)) for x in output]
Run Code Online (Sandbox Code Playgroud)