Skip to content


Hex to Unsigned Integer and Back Again

Here are some quick one line wonders. From reading some stuff here and there I was able to fashion 2 functions for handling the conversion between hex strings and unsigned integers. Most of the time a uint is asked for when it comes to color, but some things like the StyleSheet object asks for a string value of a hex color like you would use in CSS.

I guess these function names got a little long, but they are very descriptive to what they do, and when you use Flex with it’s completion function name brevity isn’t as much of an issue as it is with Flash.

function hexColorToUnsignedInteger(hex:String):uint
{
	return uint("0x" + hex.split("#")[1]);
}
 
function unsignedIntegerToHexColor(num:uint):String
{
	return String("#" + (num.toString(16).toUpperCase()));
}
 
trace(hexColorToUnsignedInteger("#FF0000"));
trace(unsignedIntegerToHexColor(0xFF0000));

Posted in ActionScript 3.0, Work. Tagged with , , .

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(never shared)

or, reply to this post via trackback.