Nodejs Base64 Encode toBase64()

Here you can find the source of toBase64()

Method Source Code

//Rules//from   w w  w  . j av a  2s  . c  o  m
/*
Extend the String object to create a function that converts the value of the String to and from Base64 using the ASCII character set.

Usage:

// should return 'dGhpcyBpcyBhIHN0cmluZyEh'
'this is a string!!'.toBase64();

// should return 'this is a string!!'
'dGhpcyBpcyBhIHN0cmluZyEh'.fromBase64();
*/


//My Code
String.prototype.toBase64 = function() {
  var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  var encoded = '';

  for(var i=0; i < this.length; i+=3) {
    var c1 = this.charCodeAt(i),
        c2 = this.charCodeAt(i+1),
        c3 = this.charCodeAt(i+2);
    encoded += chars[(c1 & 0xFC) >> 2];
    encoded += chars[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
    encoded += chars[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)];
    encoded += chars[c3 & 0x3F];
  }

  return encoded;
};
String.prototype.fromBase64 = function() {
  var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  var decoded = '';

  for(var i=0; i < this.length; i+=4) {
    var c1 = chars.indexOf(this[i]),
        c2 = chars.indexOf(this[i+1]),
        c3 = chars.indexOf(this[i+2]),
        c4 = chars.indexOf(this[i+3]);
    decoded += String.fromCharCode(((c1) << 2) | ((c2 & 0x30) >> 4));
    decoded += String.fromCharCode(((c2 & 0x0F) << 4) | ((c3 & 0xFC) >> 2));
    decoded += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  }

  return decoded;
};


//Tests
'this is a string!!'.toBase64(); //should return 'dGhpcyBpcyBhIHN0cmluZyEh'
'dGhpcyBpcyBhIHN0cmluZyEh'.fromBase64(); //should return 'this is a string!!'

Related

  1. toBase64()
    String.prototype.toBase64 = function () {
        var converter = new Buffer(this.toString(), 'utf8');
        return converter.toString('base64');
    
  2. toBase64()
    const CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
    String.prototype.toBase64 = function() {
      let out = ''
      for (let i = 0, length = this.length; i < length; i += 3) {
        let [a, b, c] = [...this.slice(i, i + 3)].map(c => c.charCodeAt(0))
        bits = a << 16 | b << 8 | c
        out += CODES[bits >> 18 & 0x3F]
        out += CODES[bits >> 12 & 0x3F]
        out += b ? CODES[bits >> 6 & 0x3F] : '='
    ...
    
  3. toBase64()
    String.prototype.toBase64 = function() {
      var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
      var output = ""
      var chr1, chr2, chr3
      var enc1, enc2, enc3, enc4
       var i = 0
       do {
          chr1 = this.charCodeAt(i++)
          chr2 = this.charCodeAt(i++)
    ...
    
  4. toBase64()
    String.prototype.toBase64 = function(){
      var nToOrd = function(n){
        return n < 26 ? "A".charCodeAt(0) + n : (
          n < 52 ? "a".charCodeAt(0) - 26 + n : (
            n < 62 ? "0".charCodeAt(0) - 52 + n : (
              n==62 ? "+".charCodeAt(0) : "/".charCodeAt(0)
            )
          )
        );
    ...
    
  5. toBase64()
    var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
    String.prototype.toBase64 = function() {
      var encoded = [];
      var a, b, c;
      for (var i = 0, len = this.length; i < len; i = i + 3) {
        a = this.charCodeAt(i);
        encoded.push(code[a >> 2]);
        b = this.charCodeAt(i + 1);
        if (isNaN(b)) {
    ...
    
  6. toBase64()
    String.prototype.toBase64 = function () {
      var stringyBits = this.match(/.{1,3}/g);
      var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
      result = stringyBits.map(function(threeLetters) {
       var letters = threeLetters.split("");
        var ascii3 = letters.map(function(char) {
          return pad(char.charCodeAt(0).toString(2),8);
        });
        var twentyFour3 = ascii3[0]+ascii3[1]+ascii3[2];
    ...