msgbartop
Code Musings and Such
msgbarbottom

13 Jun 09 Converting Fractional Hex to Decimal in ActionScript

I'm working on a little Flex Geohashing application which required me to convert a fractional Hex value to decimal and the net was no help. It's a trivial algorithm, but it's not the kind of thing I want to spend my time writing. So, Here you go, future!

public function fractionalHexToDecimal(hex:String):Number {
	var total:Number = 0;
	for(var i:int = 0; i < hex.length; i++) {
		total += (parseInt("0x" + hex.charAt(i)) / Math.pow(16,i+1));
	}
	return total;
}

Only pass in the fractional. No "0x". No decimal point. Here's how it do.

fractionalHexToDecimal("1");
// 0.0625

fractionalHexToDecimal("0a");
// 0.0390625

fractionalHexToDecimal("db9318c2259923d0");
// 0.8577132677070023

Geohashing App forthcoming.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Tags: ,

Leave a Comment