Posted By

wizard04 on 07/28/08


Tagged

javascript number base radix


Versions (?)

Number Base Conversion


 / Published in: JavaScript
 

Convert a number to a different base.

  1. //Convert a number to a different base (e.g., from hex to decimal)
  2. function changeBase(num, from, to)
  3. {
  4. if(isNaN(from) || from < 2 || from > 36 || isNaN(to) || to < 2 || to > 36)
  5. throw (new RangeError("Illegal radix. Radices must be integers between 2 and 36, inclusive."));
  6. num = parseInt(num, from); //convert to decimal
  7. num = num.toString(to); //convert the decimal to desired base
  8. return num.toUpperCase();
  9. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wizard04 on August 6, 2008

Completely rewrote it.

Posted By: cbecker333 on July 16, 2009

This does not work. The base=>decimal conversion doesn't work correctly. I found several instances where base4=>base64 conversion absolutely does not work.

Posted By: wizard04 on July 17, 2009

Could you give me a couple examples so I can fix it?

Posted By: wizard04 on December 8, 2009

Made this MUCH simpler!

Posted By: wizard04 on April 16, 2014

The latest: https://gist.github.com/wizard04wsu/8827255

You need to login to post a comment.