Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 995
quakkels Avatar asked Feb 13 '15 13:02

quakkels


2 Answers

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=='
like image 123
Martijn Pieters Avatar answered Oct 02 '22 04:10

Martijn Pieters


If you are looking for a way to get the encoded string without the trailing newline the base64.b46encode function will do that. Here's the difference:

In [19]: base64.encodestring('a')
Out[19]: 'YQ==\n'

In [20]: base64.b64encode('a')
Out[20]: 'YQ=='
like image 34
André Laszlo Avatar answered Oct 02 '22 05:10

André Laszlo