Javascript String toBase64()

Description

Javascript String toBase64()


// https://www.codewars.com/kata/base64-encoding/train/javascript

// Extend the String object with toBase64() and fromBase64() functions
String.prototype.toBase64 = function () {
  return new Buffer(`${this}`).toString('base64');
};

Javascript String toBase64()

String.prototype.toBase64 = function() {
    return (new Buffer(this.toString()).toString('base64'));
}

String.prototype.fromBase64 = function() {
    return new Buffer(this.toString(), 'base64').toString('utf8');

}
console.log(new Buffer("Hello World").toString('base64'));
console.log("Hello World".toBase64());

Javascript String toBase64()

var util = require('util');
String.prototype.toBase64 = function () {
    var converter = new Buffer(this.toString(), 'utf8');
    return converter.toString('base64');
}

String.prototype.fromBase64 = function () {
    var converter = new Buffer(this.toString(), 'base64');
    return converter.toString('utf8');
}

Javascript String toBase64()

// http://www.codewars.com/kata/base64-encoding

String.base64Map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

String.prototype.toBase64 = function() {
  return this.split('').map(c => {
    return c.charCodeAt(0).toString(2).pad('0', 8);
  }).join('')/*w w  w.j av a  2  s.c o m*/
    .replace(/.{1,6}/g, b => {
      return String.base64Map[parseInt(b, 2)];
    })
};

String.prototype.fromBase64 = function() {
  return this.split('').map(c => {
    return String.base64Map.indexOf(c).toString(2).pad('0', 6);
  }).join('')
    .replace(/.{1,8}/g, b => {
      return String.fromCharCode(parseInt(b, 2));
    });
};

String.prototype.pad = function(c, n) {
  if (this.length >= n) {
    return this;
  }
  
  let len = n - this.length;
  let s = this;
  while (len--) {
    s = c + s;
  }
  return s;
};

Javascript String toBase64()

// Extend the String object with toBase64() and fromBase64() functions
// Long way//from www  .  j ava2s  .  c o m
// String.prototype.toBase64 = function(){
  // return this
    // .split('')
    // .map(el => el.charCodeAt())
    // .map(el => el.toString(2))
    // .map(el => {
      // let len = el.length;
      // let amend = '0';
      // let result = el;
      // while(len < 8) result = amend + result;
      // return result;
    // })
    // .map((el, idx, arr) => {
      // if(idx % 5 === 0) return arr.slice(idx - 5, idx + 1).join('')
    // })
    // .map(el => parseInt(el, 2))
// }

// Short way
// Extend the String object with toBase64() and fromBase64() functions
String.prototype.toBase64 = function(){
  return (new Buffer(this + '')).toString('base64');
}

String.prototype.fromBase64 = function(){
  return (new Buffer((this + ''), 'base64').toString('ascii'));
}

module.exports = String;

Javascript String 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] : '='
    out += c ? CODES[bits & 0x3F] : '='
  }//w  w  w. ja v  a2s.c  om
  return out
}

String.prototype.fromBase64 = function() {
  let out = ''
  for (let i = 0, length = this.length; i < length; i += 4) {
    let [a, b, c, d] = [...this.slice(i, i + 4)].map(c => CODES.indexOf(c))
    bits = a << 18 | b << 12 | c << 6 | d
    out += String.fromCharCode(bits >> 16)
    if (c !== 64) out += String.fromCharCode(bits >> 8 & 0xFF)
    if (d !== 64) out += String.fromCharCode(bits & 0xFF)
  }
  return out
}

Javascript String toBase64()

const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

String.prototype.toBase64 = function toBase64() {
  let bits = '';
  const len = this.length;
  for (let i = 0; i < len; i++) {
    const bit = `00000000${this.charCodeAt(i).toString(2)}`;
    bits += bit.substr(-8);/*w  ww .  j  av a2  s  .co  m*/
  }
  if (len % 3 === 1) {
    bits += '0000000000000000'; // 16 0s
  } else if (len % 3 === 2) {
    bits += '00000000';
  }

  let ans = '';
  for (let i = 0; i < bits.length; i += 6) {
    const s = bits.slice(i, i + 6);
    const n = parseInt(s, 2);
    ans += table[n];
  }
  return ans;
};

String.prototype.fromBase64 = function fromBase64() {
  let bits = '';
  for (let i = 0; i < this.length; i++) {
    const n = table.indexOf(this[i]);
    const s = `000000${n.toString(2)}`;
    bits += s.substr(-6);
  }

  let ans = '';
  for (let i = 0; i < bits.length; i += 8) {
    const s = bits.slice(i, i + 8);
    const n = parseInt(s, 2);
    ans += String.fromCharCode(n);
  }
  return ans;
};



PreviousNext

Related