Reputation: 15
I have a set of base64 encoded strings. When I try to decode the same, the decoded text just has the last line of the actual string. On any online base64 decoder, the entire text is displayed. I have tried with and without encoding the text to utf-8 prior to decoding. Here is my code:
import base64
encodedStr = "QXJyaXZhbCBNZXNzYWdlDURhdGUgKFVUQyk6IDI2SlVMMjAN"
#with encoding
encodedstr_bytes = encodedStr.encode('utf-8')
decodedBytes = base64.b64decode(encodedstr_bytes)
decodedStr = decodedBytes.decode('utf8')
print(decodedStr)
#without encoding
decodedBytes = base64.b64decode(encodedStr)
decodedStr = decodedBytes.decode("utf-8")
print(decodedStr)
Output :
Date (UTC): 26JUL20
Date (UTC): 26JUL20
Required Output :
Arrival Message
Date (UTC): 26JUL20
Upvotes: 1
Views: 1378
Reputation: 388
Your string has a carriage return ("\r"), but no linefeed ("\n"). On Windows, this instructs the printer to return to the start of the line and overwrite what is there. The following code behaves the same:
print("foo\rbar") # Prints "bar"
print("fooqux\rbar") # Prints "barqux"
Upvotes: 2