index.js
function bintodecoutput(bin){
  var len = bin.length;
  var decoutput = String();
  var work = new Array(len);
  var outputlen = 0;
  for(var i=0; i<len; i++){work[i] = bin[i];}
  while(len){
    // as long as a remaining value exists
    var lead = false;
    var bit0;
    var bit1;
    var bit2;
    var bit3;
    var value;
    var i = 0;
    while(i < len-3){
      // walk through the remaining value
      bit0 = work[i+3];
      bit1 = work[i+2];
      bit2 = work[i+1];
      bit3 = work[i+0];
      value = (bit3<<3) | (bit2<<2) | (bit1<<1) | (bit0<<0);
      if(value >= 10){
        // For nibbles greaterequal than 10, adjust the bits accordingly.
        work[i+0] = true; work[i+1] = bit2 && bit1; work[i+2] = !bit1;
      }else{
        work[i+0] = lead;
        if(lead){
          // When the previous nibble was 8 or 9, adjust the bits accordingly
          work[i+1] = !bit1; work[i+2] = !bit1;
          lead = bit1;
        }else{
          // otherwise, just leave the bits as they are.
          if(value >= 8){lead = true;}
        }
      }
      i++;
    }
    // extract the decimal value of the remaining bits
    if(len==1){
      bit0 = work[0];
      bit1 = false;
      bit2 = false;
    }else if(len==2){
      bit0 = work[1];
      bit1 = work[0];
      bit2 = false;
    }else{
      bit0 = work[i + 2];
      bit1 = work[i + 1];
      bit2 = work[i + 0];
    }
    bit3 = lead;
    var value = (bit3<<3) | (bit2<<2) | (bit1<<1) | (bit0<<0);
    if(!(outputlen%3)){decoutput = ' ' + decoutput;}
    decoutput = value + decoutput;
    outputlen++;
    len = i;
  }
  // Remove zeros
  var i = 0;
  outputlen = decoutput.length;
  while((i < outputlen) && ((decoutput[i] == '0') || (decoutput[i] == ' '))){i++;}
  if(i == outputlen){
    return "0";
  }else{
    return decoutput.slice(i);
  }
}
function bintodecoutput(bin){
  var len = bin.length;
  var decoutput = String();
  var work = new Array(len);
  var outputlen = 0;
  for(var i=0; i<len; i++){work[i] = bin[i];}
  while(len){
    // as long as a remaining value exists
    var lead = false;
    var bit0;
    var bit1;
    var bit2;
    var bit3;
    var value;
    var i = 0;
    while(i < len-3){
      // walk through the remaining value
      bit0 = work[i+3];
      bit1 = work[i+2];
      bit2 = work[i+1];
      bit3 = work[i+0];
      value = (bit3<<3) | (bit2<<2) | (bit1<<1) | (bit0<<0);
      if(value >= 10){
        // For nibbles greaterequal than 10, adjust the bits accordingly.
        work[i+0] = true; work[i+1] = bit2 && bit1; work[i+2] = !bit1;
      }else{
        work[i+0] = lead;
        if(lead){
          // When the previous nibble was 8 or 9, adjust the bits accordingly
          work[i+1] = !bit1; work[i+2] = !bit1;
          lead = bit1;
        }else{
          // otherwise, just leave the bits as they are.
          if(value >= 8){lead = true;}
        }
      }
      i++;
    }
    // extract the decimal value of the remaining bits
    if(len==1){
      bit0 = work[0];
      bit1 = false;
      bit2 = false;
    }else if(len==2){
      bit0 = work[1];
      bit1 = work[0];
      bit2 = false;
    }else{
      bit0 = work[i + 2];
      bit1 = work[i + 1];
      bit2 = work[i + 0];
    }
    bit3 = lead;
    var value = (bit3<<3) | (bit2<<2) | (bit1<<1) | (bit0<<0);
    if(!(outputlen%3)){decoutput = ' ' + decoutput;}
    decoutput = value + decoutput;
    outputlen++;
    len = i;
  }
  // Remove zeros
  var i = 0;
  outputlen = decoutput.length;
  while((i < outputlen) && ((decoutput[i] == '0') || (decoutput[i] == ' '))){i++;}
  if(i == outputlen){
    return "0";
  }else{
    return decoutput.slice(i);
  }
}
function bintodecoutput ( bin ) { var len = bin . length ; var decoutput = String ( ) ; var work = new Array ( len ) ; var outputlen = 0 ; for ( var i = 0 ; i < len ; i ++ ) { work [ i ] = bin [ i ] ; } while ( len ) { // as long as a remaining value exists var lead = false ; var bit0 ; var bit1 ; var bit2 ; var bit3 ; var value ; var i = 0 ; while ( i < len - 3 ) { // walk through the remaining value bit0 = work [ i + 3 ] ; bit1 = work [ i + 2 ] ; bit2 = work [ i + 1 ] ; bit3 = work [ i + 0 ] ; value = ( bit3 << 3 ) | ( bit2 << 2 ) | ( bit1 << 1 ) | ( bit0 << 0 ) ; if ( value >= 10 ) { // For nibbles greaterequal than 10, adjust the bits accordingly. work [ i + 0 ] = true ; work [ i + 1 ] = bit2 && bit1 ; work [ i + 2 ] = ! bit1 ; } else { work [ i + 0 ] = lead ; if ( lead ) { // When the previous nibble was 8 or 9, adjust the bits accordingly work [ i + 1 ] = ! bit1 ; work [ i + 2 ] = ! bit1 ; lead = bit1 ; } else { // otherwise, just leave the bits as they are. if ( value >= 8 ) { lead = true ; } } } i ++ ; } // extract the decimal value of the remaining bits if ( len == 1 ) { bit0 = work [ 0 ] ; bit1 = false ; bit2 = false ; } else if ( len == 2 ) { bit0 = work [ 1 ] ; bit1 = work [ 0 ] ; bit2 = false ; } else { bit0 = work [ i + 2 ] ; bit1 = work [ i + 1 ] ; bit2 = work [ i + 0 ] ; } bit3 = lead ; var value = ( bit3 << 3 ) | ( bit2 << 2 ) | ( bit1 << 1 ) | ( bit0 << 0 ) ; if ( ! ( outputlen % 3 ) ) { decoutput = ' ' + decoutput ; } decoutput = value + decoutput ; outputlen ++ ; len = i ; } // Remove zeros var i = 0 ; outputlen = decoutput . length ; while ( ( i < outputlen ) && ( ( decoutput [ i ] == '0' ) || ( decoutput [ i ] == ' ' ) ) ) { i ++ ; } if ( i == outputlen ) { return "0" ; } else { return decoutput . slice ( i ) ; } }
Formats the given binary array to a decimal output string
Formats the given binary array to a decimal output string