Here is my code:
clear = hex_string.decode("hex")
base64 = clear.encode("base64")
hexstring = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
result = hex_to_base64(hexstring)
# verify results
if result == 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t':
print("Yuuuup!!! %r" % result)
print("Nope! %r" % result)
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 (
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=='