doc/snip/numericbase [en]

All languages

Differences between version EMPTY and #8387

0a1,35
> <code haxe>
> class BaseUtil 
> {
> 
> 	private static var BASE_CHARS:String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
> 	
> 	public static function toNumericBase(input:Int, base:Int):String
> 	{
> 		var result:String = "";
> 		while ( input > 0)
> 		{
> 			result = BASE_CHARS.charAt(input % base) + result;
> 			input = Std.int(input / base);
> 		}
> 		return result;
> 	}
> 	
> 	public static function toDecimal(input:String, base:Int):Int
> 	{
> 		input = input.toUpperCase();
> 		var i:Int, len:Int;
> 		i = len = input.length;
> 		var result:Int = 0;
> 		
> 		while (i-- > 0)
> 		{
> 			result += Std.int(Math.pow(base, i) * BASE_CHARS.indexOf(input.charAt(len - i - 1)));
> 		}
> 		
> 		return result;
> 	}
> }
> </code>
> 
> Calculates the base x representation of a decimal value and vice versa.
\ No newline at end of file

	
Ver Date User Action
#8387 2010-04-06 00:12:27 BernardV View | Diff

Previous | Next