Base64 encoding vs Ascii85 encoding Base64 encoding vs Ascii85 encoding json json

Base64 encoding vs Ascii85 encoding


Base64 is way more common. The difference in size really isn't that significant in most cases, and if you add at the HTTP level (which will compress the base64) instead of within your payload, you may well find the difference goes away entirely.

Are there any problems with Ascii85 that mean that I should use Base64 instead?

I would strongly advise using base64 just because it's so much more widespread. It's pretty much the canonical way of representing binary data as text (unless you want to use hex, of course).


ASCII85 is a nice encoding to use to save that extra bit of space. But it outputs many characters that would need to be escaped if naively sent over HTTP. Base64 encoding has a variant that can be sent over HTTP without any escaping.

Here's a javascript ASCII85 encoder in case anyone needs to try:

// By Steve Hanov. Released to the public domain.function encodeAscii85(input) {  var output = "<~";  var chr1, chr2, chr3, chr4, chr, enc1, enc2, enc3, enc4, enc5;  var i = 0;  while (i < input.length) {    // Access past the end of the string is intentional.    chr1 = input.charCodeAt(i++);    chr2 = input.charCodeAt(i++);    chr3 = input.charCodeAt(i++);    chr4 = input.charCodeAt(i++);    chr = ((chr1 << 24) | (chr2 << 16) | (chr3 << 8) | chr4) >>> 0;    enc1 = (chr / (85 * 85 * 85 * 85) | 0) % 85 + 33;    enc2 = (chr / (85 * 85 * 85) | 0) % 85 + 33;    enc3 = (chr / (85 * 85) | 0 ) % 85 + 33;    enc4 = (chr / 85 | 0) % 85 + 33;    enc5 = chr % 85 + 33;    output += String.fromCharCode(enc1) +      String.fromCharCode(enc2);    if (!isNaN(chr2)) {      output += String.fromCharCode(enc3);      if (!isNaN(chr3)) {        output += String.fromCharCode(enc4);        if (!isNaN(chr4)) {          output += String.fromCharCode(enc5);        }      }    }  }  output += "~>";  return output;}
<input onKeyUp="result.innerHTML = encodeAscii85(this.value)" placeholder="write text here" type="text"><p id="result"></p>