如何将bytearray('b\x9e\x18K\x9a')
转换为类似的内容 - > \x9e\x18K\x9a
< ---只是str,不是数组!
>> uidar = bytearray()
>> uidar.append(tag.nti.nai.uid[0])
>> uidar.append(tag.nti.nai.uid[1])
>> uidar.append(tag.nti.nai.uid[2])
>> uidar.append(tag.nti.nai.uid[3])
>> uidar
bytearray('b\x9e\x18K\x9a')
我尝试通过
解码我的bytearrayuid = uidar.decode('utf-8')
但它不能......
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
uid = uidar.decode("utf-8")
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9e in position 0: invalid start byte
请帮帮我......
答案 0 :(得分:13)
在2.x中,字符串是字节串。
>>> str(bytearray('b\x9e\x18K\x9a'))
'b\x9e\x18K\x9a'
Latin-1将前256个字符映射到它们的bytevalue等价物,因此在Python 3.x中:
3>> bytearray(b'b\x9e\x18K\x9a').decode('latin-1')
'b\x9e\x18K\x9a'