- 论坛徽章:
- 0
|
事情是这样的: struct.pack在python3上返回个bytes,我试着解码成utf-8 或者ascii都报错,这可咋办
在python2上:- >>> msg = struct.pack("I",socket.htonl(len(text))) + text
- >>>
复制代码 在python3上:- >>> msg = struct.pack("I",socket.htonl(len(text))) + text
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- TypeError: can't concat bytes to str
- >>>
复制代码- >>> struct.pack("<I",socket.htonl(len(text))).decode('utf-8')
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xac in position 3: invalid start byte
复制代码
- >>> struct.pack("<I",socket.htonl(len(text))).decode('ascii')
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xac in position 3: ordinal not in range(128)
- >>>
复制代码 |
|