Skip to content

Instantly share code, notes, and snippets.

View mantonaci's full-sized avatar

Michele Antonaci mantonaci

View GitHub Profile
@mantonaci
mantonaci / gist:10731724
Created April 15, 2014 13:16
Convert byte array to base64 string
var i2a = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'];
exports.base64_encode = function(s) {
var length = s.length;
var groupCount = Math.floor(length / 3);
var remaining = length - 3 * groupCount;
var result = "";
var idx = 0;
for (var i = 0; i < groupCount; i++) {