Convert from one base to another in JavaScript

自闭症网瘾萝莉.ら 提交于 2019-12-07 16:50:02

问题


In JavaScript, is there any built-in function for converting an integer from one given base to another given base? I've noticed that it's already possible to convert a decimal number to another base using toString(numberToConvertTo), but I haven't 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
}

回答1:


Call parseInt(str, fromBase) to convert to base 10 (or rather, to an actual number), then call num.toString(toBase).




回答2:


You might want to customise your input or output. You might want more than 36 bases. So I made 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 customise the input chars so you can do things like this

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

and of coarse the output chars

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

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



来源:https://stackoverflow.com/questions/14448104/convert-from-one-base-to-another-in-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!