目前我有一个字符串的字节流,由我的Python代码接收,必须转换为字符串。现在,我设法提取每个字符,转换它们并将它们单独附加到字符串中。代码如下所示:
import struct
# The byte stream is received and stored in byte_stream
text = ''
i = 0
while i < len(byte_stream):
text = text + struct.unpack('c', byte_stream[i])[0]
i += 1
print(text)
但这肯定不是最有效的方法...有没有更优雅的方式来达到相同的结果?
From Convert bytes to a Python 字符串:
byte_stream = [112, 52, 52]
''.join(map(chr, bytes))
>> p44