-
-
Save Rizwan-Hasan/0f61bde81d6fa3600a2fd0af18b8ed94 to your computer and use it in GitHub Desktop.
Base64 encode & decode (only for numbers) in JavaScript
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 Base64 = (function () { | |
var ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | |
var Base64 = function () {}; | |
var _encode = function (value) { | |
if (typeof(value) !== 'number') { | |
throw 'Value is not number!'; | |
} | |
var result = '', mod; | |
do { | |
mod = value % 64; | |
result = ALPHA.charAt(mod) + result; | |
value = Math.floor(value / 64); | |
} while(value > 0); | |
return result; | |
}; | |
var _decode = function (value) { | |
var result = 0; | |
for (var i = 0, len = value.length; i < len; i++) { | |
result *= 64; | |
result += ALPHA.indexOf(value[i]); | |
} | |
return result; | |
}; | |
Base64.prototype = { | |
constructor: Base64, | |
encode: _encode, | |
decode: _decode | |
}; | |
return Base64; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment