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)
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 À
PS I don't understand why encode
didn't throw an error, some of those bytes are outside of the ASCII range.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.