Return to Snippet

Revision: 7528
at December 8, 2009 12:19 by wizard04


Updated Code
//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();
}

Revision: 7527
at August 6, 2008 14:48 by wizard04


Updated Code
//Convert a number to a different base
function changeBase(num, toBase, fromBase)
{
	fromBase = fromBase || 10;
	if(isNaN(toBase) || isNaN(fromBase)) return "";
	if(toBase < 2 || toBase > 36 || fromBase < 2 || fromBase > 36) return "";	//out of accepted range
	num = (""+num).toUpperCase();	//make sure it's a string
	if(!(/^[0-9A-Z]+$/i).test(num)) return "";	//invalid characters
	
	function toDec(c){ return !isNaN(c) ? 1*c : c.charCodeAt(0)-55; };
	var fromDec = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	//first convert to a decimal
	var d, dec = 0;
	while(num!="")
	{
		d = toDec(num[0]);
		if(d >= fromBase) return "";	//invalid character
		dec = dec*fromBase + d;
		num = num.slice(1);
	}
	
	//then convert to base toBase
	while(dec > 0)
	{
		num = fromDec[dec%toBase] + num;
		dec = Math.floor(dec/toBase);
	}
	
	return num;
}

Revision: 7526
at August 6, 2008 14:47 by wizard04


Updated Code
//Convert a number to a different base
function changeBase(num, toBase, fromBase)
{
	fromBase = fromBase || 10;
	if(isNaN(toBase) || isNaN(fromBase)) return "";
	if(toBase < 2 || toBase > 36 || fromBase < 2 || fromBase > 36) return "";	//out of accepted range
	num = (""+num).toUpperCase();	//make sure it's a string
	if(!(/^[0-9A-Z]+$/i).test(num)) return "";	//invalid characters
	
	function toDec(c){ return !isNaN(c) ? 1*c : c.charCodeAt(0)-55; };
	var fromDec = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	//first convert to a decimal
	var dec = 0;
	while(num!="")
	{
		d = toDec(num[0]);
		if(d >= fromBase) return "";	//invalid character
		dec = dec*fromBase + d;
		num = num.slice(1);
	}
	
	//then convert to base toBase
	while(dec > 0)
	{
		num = fromDec[dec%toBase] + num;
		dec = Math.floor(dec/toBase);
	}
	
	return num;
}

Revision: 7525
at August 6, 2008 14:45 by wizard04


Updated Code
//Convert a number to a different base
function changeBase(num, toBase, fromBase)
{
	fromBase = fromBase || 10;
	if(isNaN(toBase) || isNaN(fromBase)) return "";
	if(toBase < 2 || toBase > 36 || fromBase < 2 || fromBase > 36) return "";	//out of accepted range
	num = (""+num).toUpperCase();	//make sure it's a string
	if(!(/^[0-9A-Z]+$/i).test(num)) return "";	//invalid characters
	
	function toDec(c){ return !isNaN(c) ? 1*c : c.charCodeAt(0)-55; };
	var fromDec = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	//first convert to a decimal
	var dec = 0;
	while(num!="")
	{
		d = toDec(num.charAt(0));
		if(d >= fromBase) return "";	//invalid character
		dec = dec*fromBase + d;
		num = num.slice(1);
	}
	
	//then convert to base toBase
	while(dec > 0)
	{
		num = fromDec[dec%toBase] + num;
		dec = Math.floor(dec/toBase);
	}
	
	return num;
}

Revision: 7524
at August 6, 2008 14:40 by wizard04


Updated Code
//Convert a number to a different base
function changeBase(num, toBase, fromBase)
{
	fromBase = fromBase || 10;
	if(isNaN(toBase) || isNaN(fromBase)) return "";
	if(toBase < 2 || toBase > 36 || fromBase < 2 || fromBase > 36) return "";	//out of accepted range
	num = (""+num).toUpperCase();	//make sure it's a string
	if(!(/^[0-9A-Z]+$/i).test(num)) return "";	//invalid characters
	
	function toDec(c)
	{
		if(!isNaN(c)) return 1*c;
		return c.charCodeAt(0)-55;
	}
	var fromDec = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	//first convert to a decimal
	var dec = 0;
	while(num!="")
	{
		d = toDec(num.charAt(0));
		if(d >= fromBase) return "";	//invalid character
		dec = dec*fromBase + d;
		num = num.slice(1);
	}
	
	//then convert to base toBase
	while(dec > 0)
	{
		num = fromDec[dec%toBase] + num;
		dec = Math.floor(dec/toBase);
	}
	
	return num;
}

Revision: 7523
at July 29, 2008 09:59 by wizard04


Updated Code
function changeBase(num, toBase, fromBase)
{
	if(isNaN(num) || !toBase || (fromBase && isNaN(fromBase))) return "";
	num = ""+num.toUpperCase();	//make sure it's a string
	if(!fromBase) fromBase = 10;
	var digit = "";
	
	//first, convert num to decimal
	if(fromBase == 10)
	{
		dec = 1*num;
		if(isNaN(dec)) return "";	//invalid characters
		num = "";
	}
	else
	{
		if(!(/^[0-9A-Z]+$/i).test(num)) return "";	//invalid characters
		var dec = 0;
		do
		{
			digit = num.charAt(0);
			if(!isNaN(digit)) dec = dec*fromBase + 1*digit;
			else if(digit.charCodeAt(0)-55 >= fromBase) return "";	//invalid character
			else dec = dec*fromBase + digit.charCodeAt(0)-55;
			num = num.slice(1);
		}while(num != "")
	}
	
	//then convert to base toBase
	if(toBase > 36) return "";	//base is too big; not enough letters in the alphabet
	digit = "";
	do
	{
		digit = dec % toBase;
		if(digit <= 9) num = digit.toString() + num;
		else num = String.fromCharCode(digit+55) + num;
		dec = (dec - dec%toBase) / toBase;
	}while(dec > 0)
	
	return num;
}

Revision: 7522
at July 28, 2008 12:31 by wizard04


Updated Code
function changeBase(num, toBase, fromBase)
{
	if(isNaN(num) || !toBase) return "";
	num = ""+num;	//make sure it's a string
	if(!fromBase) fromBase = 10;
	var digit = "";
	
	//first, convert num to decimal
	if(fromBase == 10)
	{
		dec = 1*num;
		if(isNaN(dec)) return "";	//invalid characters
		num = "";
	}
	else
	{
		if(!(/^[0-9A-Z]+$/i).test(num)) return "";	//invalid characters
		var dec = 0;
		do
		{
			digit = num.charAt(0);
			if(!isNaN(digit)) dec = dec*fromBase + 1*digit;
			else if(digit.charCodeAt(0)-55 >= fromBase) return "";	//invalid character
			else dec = dec*fromBase + digit.charCodeAt(0)-55;
			num = num.slice(1);
		}while(num != "")
	}
	
	//then convert to base toBase
	if(toBase > 36) return "";	//base is too big; not enough letters in the alphabet
	digit = "";
	do
	{
		digit = dec % toBase;
		if(digit <= 9) num = digit.toString() + num;
		else num = String.fromCharCode(digit+55) + num;
		dec = (dec - dec%toBase) / toBase;
	}while(dec > 0)
	
	return num;
}

Revision: 7521
at July 28, 2008 12:10 by wizard04


Initial Code
Number.prototype.toBase = function(base)
{
	if(base > 36) return "";	//base is too big; not enough letters in the alphabet
	var n = this.valueOf();
	var result = "";
	var digit = "";
	do
	{
		digit = n % base;
		if(digit <= 9) result = digit.toString() + result;
		else result = String.fromCharCode(digit+55) + result;
		n = (n - n%base) / base;
	}while(n > 0)
	return result;
};
String.prototype.numberFromBase = function(base)
{
	if(!(/^[0-9A-Z]+$/i).test(this)) return NaN;	//invalid characters
	var s = this.valueOf();
	var result = 0;
	var digit = "";
	do
	{
		digit = s.charAt(0);
		if(!isNaN(digit)) result = result*base + 1*digit;
		else if(digit.charCodeAt(0)-55 >= base) return NaN;	//invalid character
		else result = result*base + digit.charCodeAt(0)-55;
		s = s.slice(1);
	}while(s != "")
	return result;
}

Initial URL

                                

Initial Description
Convert a number to a different base.

Initial Title
Number Base Conversion

Initial Tags
javascript, number

Initial Language
JavaScript