Skip to content

Instantly share code, notes, and snippets.

@Rizwan-Hasan
Forked from alkaruno/Base64.js
Created February 14, 2021 12:42
Show Gist options
  • Save Rizwan-Hasan/0f61bde81d6fa3600a2fd0af18b8ed94 to your computer and use it in GitHub Desktop.
Save Rizwan-Hasan/0f61bde81d6fa3600a2fd0af18b8ed94 to your computer and use it in GitHub Desktop.
Base64 encode & decode (only for numbers) in JavaScript
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