Revision: 17405
Updated Code
at September 3, 2009 09:32 by bucabay
Updated Code
Math.base = function base(n, to, from) {
return parseInt(n, from || 10).toString(to);
}
Revision: 17404
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at September 3, 2009 09:28 by bucabay
Initial Code
function base(n, to, from) {
return parseInt(n, from || 10).toString(to);
}
Initial URL
http://www.bucabay.com/web-development/base-conversion-in-javascript/
Initial Description
The snippet uses the native Javascript function so it should be more efficient then for instance using arithmetic or looped bitwise operations etc.
Examples:
// convert the decimal 10 to hex
Math.base(10, 16); // 'a'
// convert the hex 'a' to decimal
Math.base('a', 10, 16); // 10
// convert the decimal 10 to binary
Math.base(10, 2); // '1010'
// convert the hex 'a' to decimal
Math.base('a', 2, 16); // 1010
[Base conversion in JavaScript][1]
[1]: http://www.bucabay.com/web-development/base-conversion-in-javascript/ "Base conversion in JavaScript"
Initial Title
Number base conversion, hex, dec, binary
Initial Tags
Initial Language
JavaScript