DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Generic Base Conversor //Javascript Object

07.29.2005
| 4267 views |
  • submit to reddit
        <a href="http://jsfromhell.com/number/base-conversor">
Minimum Common Multiple

[UPDATED CODE AND HELP CAN BE FOUND HERE]
</a>

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/number/base-conversor [v1.0]

Conversor = {
	h: '0123456789abcdefghijklmnopqrstuvwxyz',

	int2base: function( n, base ){
		if( base < 2 || base > this.h.length )
			throw new Error( "base inválida" );
		for( var n = parseInt( n ) || 0, result = ""; n; result = this.h.charAt( n % base ) + result, n = Math.floor( n / base ) );
		return result;
	},
	base2int: function( s, base ){
		for( var i = -1, l = s.length, result = 0; ++i < l; result = result * base + this.h.indexOf( s.charAt( i ) ) );
		return result;
	}
}