quakkels quakkels - 3 months ago 10
Python Question

Why is there a newline on the end of my string when I finish decoding and encoding it?

Here is my code:

def hex_to_base64(hex_string):
clear = hex_string.decode("hex")
print(clear)
base64 = clear.encode("base64")
print(base64)
return base64

hexstring = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
result = hex_to_base64(hexstring)

# verify results
if result == 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t':
print("Yuuuup!!! %r" % result)
else:
print("Nope! %r" % result)


My results verification test is failing. It prints out:

Nope! 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t\n'


Where is the '\n' newline coming from? I could strip it off to get the test to pass, but I feel like that's cheating.

Answer

The Base64 encoding includes it:

>>> 'a'.encode('base64')
'YQ==\n'

Other Base64 encoding methods also include that newline; see base64.encode() for example:

encode() returns the encoded data plus a trailing newline character ('\n').

The choice appears to be historical; The MIME Base64 content-transfer-encoding dictates that a maximum line length is used and newlines are inserted to maintain that length, but RFC 3548 state that implementations must not.

Python offers both options; you could use the base64.b64encode() function here instead:

>>> import base64
>>> base64.b64encode('a')
'YQ=='
Comments