Search code examples
javascriptradix

Convert from one base to another in JavaScript


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
}

Solution

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