Python Forum

Full Version: base64 decoding issue or bug
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi,

i am encoding data and sending the same through TCP/IP and then trying to decode the same with base64.b64decode() operations, but for some reason i am not able to decode the data when it contains padding as "==", but the same code works when it contains padding as one character "="

But same data when i try with Java decoders it decodes perfectly in both the cases.

Not able to find the issue exactly, getting exception and terminating my simulator

Is it bug in python ?

  File "C:\Python34\lib\base64.py", line 46, in _bytes_from_decode_data
    "string, not %r" % s.__class__.__name__) from None
TypeError: argument should be a bytes-like object or ASCII string, not 'dict' 

Thanks In advance.
Next time show your code, let us now how you have made it.

So I tried this with python 2 & 3

import base64
encode = base64.b64encode(b"this is a test string asdlka jlkdjasd")
decode = base64.b64decode(encode)

print(encode, end="\n")
print(decode)
Turns out that you have to use b IN PYTHON 3.X, before you write the string that you wish to encode .b64encode(b'blabla')

RESULT (Python 3.x):
Output:
b'dGhpcyBpcyBhIHRlc3Qgc3RyaW5nIGFzZGxrYSBqbGtkamFzZA==' b'this is a test string asdlka jlkdjasd'
RESULT (Python 2.7):
Output:
dGhpcyBpcyBhIHRlc3Qgc3RyaW5nIGFzZGxrYSBqbGtkamFzZA== this is a test string asdlka jlkdjasd
Read the error message.
You are trying to encode a dictionary object which is not allowed