Nodejs String from Ascii85 fromAscii85()

Here you can find the source of fromAscii85()

Method Source Code

String.prototype.fromAscii85 = function() {
    console.log(this)//from w w w  . jav a 2  s. c  o  m
    let pad = 'u', addnum = 0, s = this.substring(2, this.length - 2).replace(/\s/g, ''), str = '', res = '';
    for (let i = 0; i < s.length; i++)
        str += s[i] === 'z' ? '!!!!!' : s[i];
    while (str.length % 5 !== 0)
    {
        str += pad;
        addnum ++;
    }
    for (let i = 0; i < str.length; i += 5)
    {
        let temp = str.substr(i, 5), decode = [], sum = 0, sum2str = '';
        for (let j = 0; j < 5; j++)
        {
            let t = temp[j].charCodeAt() - 33;
            decode.push(t);
        }
        for (let i = 4; i >= 0; i--)
            sum += decode[i] * Math.pow(85, 4 - i);
        sum2str = sum.toString(2);
        while (sum2str.length % 32 !== 0)
            sum2str = '0' + sum2str;
        for (let i = 0; i < sum2str.length; i += 8)
        {
            let temp = parseInt(sum2str.substr(i, 8), 2);
            res += String.fromCharCode(temp);
        }
    }
    res = res.substr(0, res.length - addnum);
    return res;
};

Related

  1. fromAscii85()
    String.prototype.fromAscii85 = function() {
      var str = this.slice(2, -2).replace(/\s/g, ''), result = ''
      while (str.length) {
        if (str[0] == 'z') {
          result += '\u0000\u0000\u0000\u0000'
          str = str.substr(1)
        } else {
          let num = 0, j = 5, i = 4
          while (j--) {
    ...
    
  2. fromAscii85()
    String.prototype.fromAscii85 = function() {
      var ret = "";
      var str = this.replace(/\s+/g, "").replace("z", "!!!!!").slice(2, -2);
      for(var i=0; i<str.length; i+=5){
        var deficit = Math.max(0, 5+i-str.length);
        var nnn = 0;
        for(var j=0; j<5; j++){
          nnn = nnn*85 + (str.charCodeAt(i+j) || 117) - 33;
        var codes = [];
        for(var j=0; j<4; j++){
          codes.push(nnn%256);
          nnn = nnn/256 | 0;
        codes.reverse();
        var chunk = String.fromCharCode.apply(undefined, codes).slice(0, 4-deficit);
        ret+= chunk;
      return ret;
    
  3. fromAscii85()
    String.prototype.fromAscii85 = function() {
      var str = this.slice(2, -2).replace(/z/g, '!!!!!').replace(/\s/g, '');
      var decoded = '';
      for(var i = 0, len = str.length; i < len; i+=5) {
        var n = 0;
        var us = 0;
        for(var j = 4; j >= 0; j--) {
          var c = str[j+i] || (us++, 'u'),
              code = c.charCodeAt(0) - 33;
    ...