Reputation: 195
In the following code:
import base64
base64.decodebytes('DBCCOAABAMA='.encode('ascii'))
the result is
b'\x0c\x10\x828\x00\x01\x00\xc0'
The third byte is \x828
which does not fit into a single byte and lead into a problem for my script. I do not want to decode it as anything else than ASCII
.
Is there anything wrong with the initial base64 encoder? Why does this problem happen and how can I fix it?
Update:
The problem I face is the code
base64.decodebytes('DBCCOAABAMA='.encode('ascii')).decode('ascii')
leads to an error
UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 2: ordinal not in range(128)
Upvotes: 0
Views: 388
Reputation: 308130
Some bytes, if they fall in the printable character range, don't get printed as hex escapes. This little bit of code shows you what's really happening:
>>> for b in base64.decodebytes('DBCCOAABAMA='.encode('ascii')):
print(hex(b), chr(b))
0xc
0x10
0x82 ‚
0x38 8
0x0
0x1
0x0
0xc0 À
P.S. I don't understand why encode
didn't throw an error, some of those bytes are outside of the ASCII range.
Upvotes: 1