Skip to content Skip to sidebar Skip to footer

Convert Array Of Byte Values To Base64 Encoded String And Break Long Lines, Javascript (code Golf)

This JavaScript function takes an array of numbers (in the range 0-255) and converts to a base64-encoded string, then breaks long lines if necessary: function encode(data) { var

Solution 1:

I have another entry:

function encode(data)
{
    var str = String.fromCharCode.apply(null,data);
    return btoa(str).replace(/.{76}(?=.)/g,'$&\n');
}

Minified, 88 characters:

function e(d){return btoa(String.fromCharCode.apply(d,d)).replace(/.{76}(?=.)/g,'$&\n')}

Or if you want trailing newlines, 85 characters:

function e(d){return btoa(String.fromCharCode.apply(d,d)).replace(/.{1,76}/g,'$&\n')}

Solution 2:

Works in Firefox 3.6.13:

function encode(data)
{
    var str = data.reduce(function(a,b){ return a+String.fromCharCode(b) },'');
    return btoa(str).replace(/.{76}(?=.)/g,'$&\n');
}

Solution 3:

I don't have Firefox handy, so I can't try it out, but from a general string-handling perspective it looks like you have some room to improve. What you're doing is, for every byte, creating a new string one character longer than your previous one. This is an O(N^2) operation. There are a few ways to cut down N so that your algorithm runs in near-linear time:

  1. Build up strings to length 57 (this will yield a 76-char Base64 result), then perform a btoa on it and add the resulting string to your output

  2. Just like #1, only build an array of lines and call join on it to create the final output string.

  3. Use map to create an array of 1-character strings, then call join on it.

Here's some untested code for each method:

function encode(data)
{
  var output = "";
  var str = "";
  for (var i = 0; i < data.length; i++)
  {
    str += String.fromCharCode(data[i]);
    // the "&& i != data.length - 1" clause
    // keeps the extra \n off the end of the output
    // when the last line is exactly 76 characters
    if (str.length == 57 && i != data.length - 1)
    {
      output += btoa(str) + "\n";
      str = "";
    }
  }
  return output + btoa(str);
}

function encode(data)
{
  var output = [];
  var str = "";
  for (var i = 0; i < data.length; i++)
  {
    str += String.fromCharCode(data[i]);
    if (str.length == 57)
    {
      output[output.length] = btoa(str);
      str = "";
    }
  }
  if (str != "")
    output[output.length] = btoa(str);
  return output.join("\n");
}

function encode(data)
{
  var str = data.map(function (d) { return String.fromCharCode(d) }).join("");
  return btoa(str).replace(/.{76}(?=.)/g,'$&\n');
}

And here's the last one, minified (116 chars):

function e(b){return btoa(b.map(function(d){return
String.fromCharCode(d)}).join("")).replace(/.{76}(?=.)/g,'$&\n')}

Post a Comment for "Convert Array Of Byte Values To Base64 Encoded String And Break Long Lines, Javascript (code Golf)"