# Why Base64 ends with a newline? (\n)

Why everything converted to Base64 has a newline at the end? It isn't one of base characters, is it?

Style[
b64 = ExportString["some pig", "Base64"],
ShowStringCharacters -> True
]

"c29tZSBwaWc=\n"


Moreover, it seems that it doesn't matter if you drop it or not:

ImportString[#, "Base64"] & /@ {b64, StringDrop[b64, -1]}

{"some pig", "some pig"}


But it may matter in general.

Related chat discussion.

Using SO js example. Try in console

> btoa("some pig")

< "c29tZSBwaWc="

• Style[b64 = ExportString["some pig", "UUE"], ShowStringCharacters -> True] also does add some extra \n. – Karsten 7. Nov 26 '15 at 9:47
• Maybe it's an Encoding Formats thing. Did you check the official Base64 standard recommendations? – Karsten 7. Nov 26 '15 at 10:27
• This silly image shows an annoyance that maybe was intended to be avoided, but it is probably irrelevant :P. – Jacob Akkerboom Nov 26 '15 at 11:20
• From wiki: "most implementations use a CR/LF newline pair to delimit encoded lines" – Jacob Akkerboom Nov 26 '15 at 11:47
• I think that short string is to be interpreted as the last encoded line. It may be convenient to be able to assume that all lines are delimited by newlines. – Michael E2 Nov 26 '15 at 13:52

## 1 Answer

It won't happen only at the end of the encoding but rather every 76 characters resulting from the encoding.

For many old programs that couldn't handle reading long lines, introducing a newline character was introduced every certain number of characters. In computer science argot this is called text wrapping. I guess that the number 76 of characters comes from the good practice of having code lines of at most 80 characters and having 2 per side as margin (although not sure why exactly two). This choose of 76 characters (or columns) comes from the standards in RFC2045 (page 19 paragraph 5) and is also a standard in the Linux command base64.

Nevertheless, there should be an option to change this default of 76 characters and I couldn't find it anywhere.

• So it doesn't seem that 76 characters wide line is related to the new line at the end of base64 string. – Kuba Jan 24 '16 at 8:34
• Ah, you meant just at the end? Well, I'd guess that it's meant to keep the maximum of 76 even when you concatenate two encodings. – Carlos Chida Jan 24 '16 at 8:38
• That could be a valid reason but then I suppose this end new line should be there for every ExportString type. – Kuba Jan 24 '16 at 8:39
• I think it's not a margin of 2 characters at each side but rather 4 at the beginning and none at the end. This is so that you can fit your line-numbered Fortran code onto a punched card. I find this standard very sensible and useful because it helps to remind us of the arbitrary technical limitations of equipment that ceased to be used 40 years ago. – Oleksandr R. Jan 24 '16 at 14:21
• for the record , punch cards were long gone by the time base64 was developed. It was about 7bit clean transport via plain text email. – george2079 Jan 25 '18 at 14:37