Actionscript suffers like many other languages from poor string parsing commands. What would take one line of well crafted perl is… a bit more complex here.
public function hex2dec( hex:String ) : String {
var bytes:Array = [];
while( hex.length > 2 ) {
var byte:String = hex.substr( -2 );
hex = hex.substr(0, hex.length-2 );
bytes.splice( 0, 0, int("0x"+byte) );
}
return bytes.join(" ");
}
private function d2h( d:int ) : String {
var c:Array = [ '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' ];
if( d > 255 ) d = 255;
var l:int = d / 16;
var r:int = d % 16;
return c[l]+c[r];
}
public function dec2hex( dec:String ) : String {
var hex:String = "0x";
var bytes:Array = dec.split(" ");
for( var i:int = 0; i < bytes.length; i++ )
hex += d2h( int(bytes[i]) );
return hex;
}
What do these methods do? Well, hex2dec takes a hexadecimal string (and assumes you have a throwaway prefix of “0x” or “#”) and returns a space-separated list of decimal values. dec2hex does the same thing, but in reverse.
hex2dec("0xF00F04") returns “240 15 4″
dec2hex("240 15 4") returns “0xF00F04″
Also, the method doesn’t much care if you’ve got 6 hex digits or 60.
There are a few optimizations I could make on this code here, but it’s not getting called that frequently anyway. The performance gains in my application would be trivial.
And just by way of warning, this is AS3 code. It’d need a few tweaks to work reliably in AS2 or (shudder) AS1 environments. Actually, I should make a standard boilerplate for here since I’m only ever really going to present AS3.

5 comments
Comments feed for this article
July 6, 2007 at 7:49 am
.
/**
* @author: Lukasz Zmywaczyk
*
* @usage: var a1 = new RGB( 255, 0, 0xAB );
trace( a1.getRGB( ) ); // display dec number
var a2 = new RGB( );
a2.fromHex( 0xff00ff );
trace( a2.r + ', ' + a2.g + ' ' + a2.b );
*
*
*/
class RGB {
private var _r:Number;
private var _b:Number;
private var _g:Number;
public function set r (newr:Number):Void {
_r = newr;
}
public function get r ():Number {
return _r;
}
public function set g (newg:Number):Void {
_g = newg;
}
public function get g ():Number {
return _g;
}
public function set b (newb:Number):Void {
_b = newb;
}
public function get b ():Number {
return _b;
}
function RGB(red:Number, green:Number, blue:Number){
_r = red
_g = green
_b = blue
}
function fromHex( n : Number ) {
_r = n >> 16;
n = n - ( _r > 8;
n = n - ( _g
}
}
September 3, 2007 at 3:03 pm
Wilmon
Hi
it’s a bit too late to comment but just a tip, i’m not sure if this works with as2 but i think it will work with flash 8.
var i:uint = 255;
var hexValue:String = i.toString(16); <— returns ‘ff’;
it also works with the range of the uint
thanks
July 31, 2008 at 4:33 am
kc
@Wilmon
Dec to Hex is fine. You can use the built in math. But Hex to Bin??
September 18, 2008 at 5:34 pm
Chris
I’ve implemented this functionality into a class that I think is pretty kick ass for color palates and such.. Take a look!
http://labs.somethingcolorful.com/2008/09/18/color-manipulation/
January 9, 2009 at 7:19 am
Vojtech
AS 2 version:
function hex2dec( hex ) {
var bytes = new Array();
var hex=new String();
while( hex.length > 2 ) {
var byte = hex.substr( -2 );
hex = hex.substr(0, hex.length-2 );
bytes.splice( 0, 0, int(“0x”+byte) );
}
return bytes.join(” “);
}
function d2h( d ) {
var c= new Array( ‘0′, ‘1′, ‘2′, ‘3′, ‘4′, ‘5′, ‘6′, ‘7′, ‘8′,
‘9′, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’ );
if( d > 255 ) {d = 255};
var l= int( d / 16);
var r = d % 16;
var ls=c[l];
if (ls==undefined) {ls==”";}
var rs=c[r];
if (rs==undefined) {rs==”";}
return ls+rs;
}
function dec2hex( dec ) {
var hex = “0x”;
var bytes = new Array();
bytes= dec.split(” “);
for( var i = 0; i < bytes.length; i++ )
hex += d2h( int(bytes[i]) );
trace(d2h( int(bytes[i]) ));
return hex;
}