[英]Convert byte string to bytes or bytearray
I have a string as follows: 我有一个字符串如下:
b'\x00\x00\x00\x00\x07\x80\x00\x03'
How can I convert this to an array of bytes? 如何将其转换为字节数组? ... and back to a string from the bytes?
...并从字节回到一个字符串?
in python 3: 在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'
>>>
From string to array of bytes: 从字符串到字节数组:
a = bytearray.fromhex('00 00 00 00 07 80 00 03')
or 要么
a = bytearray(b'\x00\x00\x00\x00\x07\x80\x00\x03')
and back to string: 并返回字符串:
key = ''.join(chr(x) for x in a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.