Last active
August 29, 2015 14:07
-
-
Save sergeevabc/de39215853091f7d7b20 to your computer and use it in GitHub Desktop.
Base64 encode
http://jsperf.com/base64-encode/3
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ and =
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Base64novnc = { | |
encode: function(data) { | |
"use strict"; | |
var result = ''; | |
var toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split(''); | |
var length = data.length; | |
var lengthpad = (length % 3); | |
// Convert every three bytes to 4 ascii characters. | |
for (var i = 0; i < (length - 2); i += 3) { | |
result += toBase64Table[data[i] >> 2]; | |
result += toBase64Table[((data[i] & 0x03) << 4) + (data[i + 1] >> 4)]; | |
result += toBase64Table[((data[i + 1] & 0x0f) << 2) + (data[i + 2] >> 6)]; | |
result += toBase64Table[data[i + 2] & 0x3f]; | |
} | |
// Convert the remaining 1 or 2 bytes, pad out to 4 characters. | |
var j = 0; | |
if (lengthpad === 2) { | |
j = length - lengthpad; | |
result += toBase64Table[data[j] >> 2]; | |
result += toBase64Table[((data[j] & 0x03) << 4) + (data[j + 1] >> 4)]; | |
result += toBase64Table[(data[j + 1] & 0x0f) << 2]; | |
result += toBase64Table[64]; | |
} else if (lengthpad === 1) { | |
j = length - lengthpad; | |
result += toBase64Table[data[j] >> 2]; | |
result += toBase64Table[(data[j] & 0x03) << 4]; | |
result += toBase64Table[64]; | |
result += toBase64Table[64]; | |
} | |
return result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment