- 論壇徽章:
- 0
|
事情是這樣的: struct.pack在python3上返回個bytes,我試著解碼成utf-8 或者ascii都報錯,這可咋辦
在python2上:- >>> msg = struct.pack("I",socket.htonl(len(text))) + text
- >>>
復(fù)制代碼 在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
- >>>
復(fù)制代碼- >>> 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
復(fù)制代碼
- >>> 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)
- >>>
復(fù)制代碼 |
|