Bill
Bill

Reputation: 23

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

I'm trying to decode the string but getting an error, and here is part of the code:

#!/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

Upvotes: 1

Views: 4884

Answers (1)

Dmitry
Dmitry

Reputation: 2096

As Remco notes, \x99 is not valid UTF8 byte. You need to specify encoding name, for example:

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

Upvotes: 4

Related Questions