将字节串转换为字节或字节数组
Convert byte string to bytes or bytearray
我有一个字符串如下:
b'\x00\x00\x00\x00\x07\x80\x00\x03'
如何将其转换为字节数组? ...然后从字节返回字符串?
从字符串到字节数组:
a = bytearray.fromhex('00 00 00 00 07 80 00 03')
或
a = bytearray(b'\x00\x00\x00\x00\x07\x80\x00\x03')
然后回到字符串:
key = ''.join(chr(x) for x in a)
在 python 3:
>>> a=b'\x00\x00\x00\x00\x07\x80\x00\x03'
>>> b = list(a)
>>> b
[0, 0, 0, 0, 7, 128, 0, 3]
>>> c = bytes(b)
>>> c
b'\x00\x00\x00\x00\x07\x80\x00\x03'
>>>
我有一个字符串如下:
b'\x00\x00\x00\x00\x07\x80\x00\x03'
如何将其转换为字节数组? ...然后从字节返回字符串?
从字符串到字节数组:
a = bytearray.fromhex('00 00 00 00 07 80 00 03')
或
a = bytearray(b'\x00\x00\x00\x00\x07\x80\x00\x03')
然后回到字符串:
key = ''.join(chr(x) for x in a)
在 python 3:
>>> a=b'\x00\x00\x00\x00\x07\x80\x00\x03'
>>> b = list(a)
>>> b
[0, 0, 0, 0, 7, 128, 0, 3]
>>> c = bytes(b)
>>> c
b'\x00\x00\x00\x00\x07\x80\x00\x03'
>>>