Converting From Decimal to Hex in Adobe Flex / ActionScript 3.0

June 16, 2009

In my most recent Adobe Flex project, I am storing color in the database as a hexadecimal number. However, Flex’s color picker widget (mx:ColorPicker) stores color values in decimal format. Now, I could use PHP’s dechex() function to convert the number, but I’d much rather send the data over in the proper format. After much struggling, I came across the following solution (it’s ridiculously easy):

var decimalNumber:int = 123456;
var someVal:String = decimalNumber.toString(16);

Apparently, passing a value of “16″ to the toString function will convert a decimal number to hex. Go figure.

–Adam

7 Responses to “Converting From Decimal to Hex in Adobe Flex / ActionScript 3.0”

  1. Note that this method will result in truncated hex values, e.g.,

    var intVal:int = 26316;
    var hexVal:String = intVal.toString(16);

    hexVal = “66CC”;

  2. I’m confused. How is the value of “66CC” a truncation of the integer value?

  3. Thank YOU! LOL You are right. It’s so easy I never would’ve thought of that….

  4. Thanks. This was really helpful!

  5. Man this was a life saver. I actually looked for some days without finding an answer. I am doing an Intel 8085 simulator in flex and needed some values to be provided in hexadecimal. Once again thanks.

  6. Thanks! This was useful. I fixed the truncation issue by doing this:
    var hexString:String = backgroundColor_cp.selectedColor.toString( 16 );
    while( hexString.length < 6 )
    {
    hexString = '0' + hexString;
    }
    _model.backgroundColorString = '#' + hexString;

  7. Thank you very much!! :)

Leave a Reply