 andy-h
2/5/2014 - 4:18 PM

## Convert a number to a different base (e.g., from hex to decimal)

Convert a number to a different base (e.g., from hex to decimal)

``````//Convert a number to a different base (e.g., from hex to decimal)
//converts floating point numbers as well as integers
//returns NaN if `num` is not a number of the specified base (the entire string must be valid, not just the beginning like how parseInt() does it)

var rxp, letters, m, whole, part;

throw (new RangeError("Illegal radix. Radices must be integers between 2 and 36, inclusive."));
}

//convert radices to number primitives (for speed)

//construct a regular expression pattern, based on the current radix, to extract the whole part and the remainer
}
rxp = "^([0-9A]*)(?:\\.([0-9A]*))?\$";
}
else{
letters = "BCDEFGHIJKLMNOPQRSTUVWXYZ";
}

//get strings containing the whole part and the remainder
m = (new RegExp(rxp, "i")).exec(num);
if(!m) return 0/0;	///`num` is not a number of the specified base; return NaN

whole = m || "0";
part = m || "0";

//convert the number to decimal