Nodejs Base64 Decode fromBase64()

Here you can find the source of fromBase64()

Method Source Code

String.prototype.fromBase64 = function(){
  var ordToN = function(ord){
    return ord=="=".charCodeAt(0) ? 0 : (
      ord=="/".charCodeAt(0) ? 63 : (
        ord=="+".charCodeAt(0) ? 62 : (
          ord>="0".charCodeAt(0) && ord<="9".charCodeAt(0) ? ord-"0".charCodeAt(0)+52 : (
            ord>="a".charCodeAt(0) && ord<="z".charCodeAt(0) ? ord-"a".charCodeAt(0)+26 : ord-"A".charCodeAt(0)
          )/*  w  w w  .  j a va2s .co  m*/
        )
      )
    );
  }

  var ret = "";
  for(var i=0; i<this.length; i+=4){
    var nnn = (ordToN(this.charCodeAt(i)) << 18) + (ordToN(this.charCodeAt(i+1)) << 12) + (ordToN(this.charCodeAt(i+2)) << 6) + ordToN(this.charCodeAt(i+3));
    ret+= String.fromCharCode(nnn>>16, (nnn>>8)%256, nnn%256);
  }
  return ret;
}

Related

  1. fromBase64()
    String.prototype.fromBase64 = function () {
        var converter = new Buffer(this.toString(), 'base64');
        return converter.toString('utf8');
    
  2. fromBase64()
    String.prototype.fromBase64 = function () {
      var str = this;
      var output = str.replace('-', '+').replace('_', '/');
      switch (output.length % 4) {
        case 0:
          break;
        case 2:
          output += '==';
          break;
    ...
    
  3. fromBase64()
    const CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
    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)
    ...
    
  4. fromBase64()
    String.prototype.fromBase64 = function() {
      var m = {};
      var toChar = String.fromCharCode;
      code.forEach(function(value, index) {
        m[value] = index; 
      });
      var decoded = [];
      var a, b, c, d;
      for (var i = 0, len = this.length; i < len; i = i + 4) {
    ...