关于python:UnicodeDecodeError:’utf-8’编解码器无法解码位置0的字节0x99:无效的起始字节

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x99 in position 0: invalid start byte

我正在尝试解码字符串,但得到了一个错误,下面是代码的一部分:

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python
import rsa

def constLenBin(s):
    binary ="0"*(8-(len(bin(s))-2))+bin(s).replace('0b','')
    return binary

data = 'apple'
(pubkey, privkey) = rsa.newkeys(1024)
crypto = rsa.encrypt(data.encode(), pubkey)
crypto = crypto.decode()
binary = ''.join(map(constLenBin,bytearray(crypto, 'utf-8')))

Traceback (most recent call last): File"stdin", line 1, in
module UnicodeDecodeError: 'utf-8' codec can't decode byte 0x99
in position 0: invalid start byte


该说明的,是不是有效的\x99UTF8的字节。你需要指定编码的名称,例如:

1
a = b'\x99'; a = a.decode('latin-1'); print(a)