我试图解码字符串但收到错误,这是代码的一部分:
#!/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')))
回溯(最近一次呼叫最后一次):
文件" stdin",第1行,in 模块
UnicodeDecodeError:' utf-8'编解码器不能解码字节0x99 在位置0:无效的起始字节
答案 0 :(得分:2)
正如Remco所说,\x99
无效UTF8字节。您需要指定编码名称,例如:
a = b'\x99'; a = a.decode('latin-1'); print(a)