/ Published in: JavaScript
Convert a number to a different base.
Expand |
Embed | Plain Text
//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(); }
Comments
Subscribe to comments
You need to login to post a comment.

Completely rewrote it.
This does not work. The base=>decimal conversion doesn't work correctly. I found several instances where base4=>base64 conversion absolutely does not work.
Could you give me a couple examples so I can fix it?
Made this MUCH simpler!