/ Published in: JavaScript
Convert a number to a different base.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//Convert a number to a different base (e.g., from hex to decimal) function changeBase(num, from, to) { if(isNaN(from) || from < 2 || from > 36 || isNaN(to) || to < 2 || to > 36) throw (new RangeError("Illegal radix. Radices must be integers between 2 and 36, inclusive.")); num = parseInt(num, from); //convert to decimal num = num.toString(to); //convert the decimal to desired base return num.toUpperCase(); }