Converting from one base to another in JavaScript

In JavaScript, is there a built-in function to convert an integer from one given base to another given base? I noticed that it is already possible to convert a decimal number to another base using toString(numberToConvertTo)

, but I have not yet found a general purpose function that can convert from any base to any other base, as shown:

convertFrom(toConvert, baseToConvertFrom, baseToConvertTo){
    //convert the number from baseToConvertFrom to BaseToConvertTo
}

      

+3


source to share


2 answers


Call parseInt(str, fromBase)

to convert to base 10 (or rather, to the actual number), then call num.toString(toBase)

.



+9


source


You might want to customize your input or output. You may need more than 36 bases. So I did this

function baseCon(number,fromBase,toBase,fromChars,toChars) {
  if (!fromChars) {
    fromChars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    number = number.toString().toUpperCase();
  }
  if (!toChars) {
    toChars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  }
  if (typeof number != 'object') {
    number = number.toString();
  }
  if (fromBase > fromChars.length) {
    console.log('fromBase was too high');
  } else if (toBase > toChars.length) {
    console.log('toBase was too high');
  } else {
    if (fromBase == 10) {
      var base10Num = Math.min(number);
    } else {
      var base10Num = 0;
      var place = 0;
      for (var index = number.length - 1; index > -1; index--) {
        base10Num = base10Num + (fromChars.indexOf(number[index]) * Math.pow(fromBase,place));
        place++;
      }
    }
    var string = '';
    var looping = true;
    var stringLen = 0;
    var stringVal = 0;
    while (looping) {
      stringLen++;
      if (Math.pow(toBase,stringLen) == base10Num) {
        stringLen++;
        break;
      } else if (Math.pow(toBase,stringLen) >= base10Num) {
        break;
      }
    }
    for (var placePos = stringLen; placePos > 0; placePos--) {
      for (var placeVal = 0; placeVal < (toBase + 1); placeVal++) {
        if (((placeVal * Math.pow(toBase,placePos - 1)) > (base10Num - stringVal)) || (placeVal == 0) && ((base10Num - stringVal) == 0)) {
          if (!((placeVal == 0) && ((base10Num - stringVal) == 0))) {
            stringVal = stringVal + ((placeVal - 1) * Math.pow(toBase,placePos - 1));
            string = string + toChars[placeVal - 1];
          } else {
            string = string + toChars[0];
          }
          break;
        }
      }
    }
    if (stringVal == base10Num) {
      return string;
    } else {
      console.log('baseCon failed');
      return string;
    }
  }
};`

      

You can customize the input symbols so you can do things like this

> baseCon([true,true,false,true,false],2,10,[false,true]);
< "26"

      



and rough output characters

> baseCon('3942',10,8,false,['!','@','#','$','%','^','&','*']);
< "*^%&"

      

It took me a whole day and then I remembered parseInt(number,fromBase).toString(toBase);

haha

+1


source







All Articles