0

this is my base64 String "lFKiKF2o+W/vvLqddOdv2ttxWSUX/SSZEyqcdyDDb+8=".

In python2 the following code works

print("lFKiKF2o+W/vvLqddOdv2ttxWSUX/SSZEyqcdyDDb+8=".decode('base64', 'strict'))

whereas in python3 where there isnt str.decode('base64', 'strict') is not available. I tried to do the same thing in python3 as below

b64EncodeStr4 = "lFKiKF2o+W/vvLqddOdv2ttxWSUX/SSZEyqcdyDDb+8="
print(len(b64EncodeStr4))
decodedByte = base64.b64decode(bytes(b64EncodeStr4, 'ascii'))
print(decodedByte)
decodeStr = decodedByte.decode('ascii', 'strict')
print(decodeStr)

I have tried other encoding as well like utf-8, utf-16, utf-32. But nothing works. Whats the best approach here to convert base64 to regular string in python3.

2 Answers 2

0
decodeStr = decodedByte.decode('ascii', 'ignore')

https://docs.python.org/3/library/stdtypes.html#textseq

-1

In Python3, simply use base64.b64decode. (Don't forget to import base64 module.)

For example:

import base64

b64_string = "lFKiKF2o+W/vvLqddOdv2ttxWSUX/SSZEyqcdyDDb+8="
decoded_bytes = base64.b64decode(b64_string)
decoded_string = decoded_bytes.decode('latin1')

print(decoded_string)
3
  • Author was already using b64decode. Your suggested code appears to be identical to what the author provided
    – Ramhound
    Commented Aug 26, 2023 at 18:35
  • Ohh, sorry, I misunderstood the question. If you change the parameter of decode() to 'latin-1' or 'iso-8859-1', the code will working.
    – Ocaso
    Commented Aug 27, 2023 at 7:24
  • You should clarify that reason by editing the answer
    – Ramhound
    Commented Aug 27, 2023 at 8:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .