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

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

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="

  • 2
    $\begingroup$ Style[b64 = ExportString["some pig", "UUE"], ShowStringCharacters -> True] also does add some extra \n. $\endgroup$
    – Karsten7
    Nov 26, 2015 at 9:47
  • $\begingroup$ Maybe it's an Encoding Formats thing. Did you check the official Base64 standard recommendations? $\endgroup$
    – Karsten7
    Nov 26, 2015 at 10:27
  • $\begingroup$ This silly image shows an annoyance that maybe was intended to be avoided, but it is probably irrelevant :P. $\endgroup$ Nov 26, 2015 at 11:20
  • 2
    $\begingroup$ From wiki: "most implementations use a CR/LF newline pair to delimit encoded lines" $\endgroup$ Nov 26, 2015 at 11:47
  • 2
    $\begingroup$ 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. $\endgroup$
    – Michael E2
    Nov 26, 2015 at 13:52

1 Answer 1


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.

Base 64 encoding

  • $\begingroup$ So it doesn't seem that 76 characters wide line is related to the new line at the end of base64 string. $\endgroup$
    – Kuba
    Jan 24, 2016 at 8:34
  • $\begingroup$ 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. $\endgroup$ Jan 24, 2016 at 8:38
  • $\begingroup$ That could be a valid reason but then I suppose this end new line should be there for every ExportString type. $\endgroup$
    – Kuba
    Jan 24, 2016 at 8:39
  • 1
    $\begingroup$ 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. $\endgroup$ Jan 24, 2016 at 14:21
  • 1
    $\begingroup$ for the record , punch cards were long gone by the time base64 was developed. It was about 7bit clean transport via plain text email. $\endgroup$
    – george2079
    Jan 25, 2018 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.