AS3 number to hex converter

by

I’ve been working on some code for converting colors lately and getting quite into bitwise operations. In one of the demos I was making, I needed a quick and easy way to see the results of a color calculation. Of course, simply tracing a number, even if it’s in the format 0xFF, will result in a digital number string “255″. I’ve always been a little stumped about how to do this calculation but I realized that it’s actually very easy to do with the & and >>> bitwise operators.


getNumberAsHexString(255); // 0xFF
getNumberAsHexString(0xABCDEF); // 0xABCDEF
getNumberAsHexString(0x00FFCC); // 0xFFCC
getNumberAsHexString(0x00FFCC, 6); // 0x00FFCC - Uses 6 as a minimum number of hexits

I’ve added the code to the AS3lib as  as a global function. I may move it to a utility class later on. You can view the source on Google Code.

Incidentally, in a moment of bug-fixing frustration, I found this article on O.C.A.S. which describes how the same functionality is already built into the toString() method! However, the work isn’t completely wasted since my version includes the option to show leading zeroes and adds “0x” at the front of the result.

Tagged , , , , , ,
  • Pimm Hogeling

    Being a big Nintendo fan, I find myself working with bits and bytes all the time. I even listen to 8 bit music from 8bitpeoples.com (you should check that out). I can understand how a non-bit geek would write the code like you do, but I’d go for something like this:

    package org.as3lib.math
    {
    /**
    * Converts a uint into a string in the format “0x123456789ABCDEF”.
    * This function is quite useful for displaying hex colors as text.
    * The function toString(radix:uint) does a similar job but doesn’t include
    * “0x” at the beginning of the result nor does it include leading zeroes.
    *
    * @author Mims H. Wright
    *
    * @use getNumberAsHexString(255); // 0xFF
    * getNumberAsHexString(0xABCDEF); // 0xABCDEF
    * getNumberAsHexString(0x00FFCC); // 0xFFCC
    * getNumberAsHexString(0x00FFCC, 6); // 0x00FFCC
    *
    *
    * @param number The number to convert to hex. Note, numbers larger than 0xFFFFFFFF may produce unexpected results.
    * @param minimumLength The smallest number of hexits to include in the output.
    * Missing places will be filled in with 0′s.
    * e.g. getNumberAsHexString(0xFF33, 6); // results in “0x00FF33″
    * @return String representation of the number as a string starting with “0x”
    */
    public function getNumberAsHexString(number:uint, minimumLength:uint = 1):String {
    // The string that will be output at the end of the function.
    var string:String = number.toString(0×10).toUpperCase();

    // While the minimumLength argument is higher than the length of the string, add a leading zero.
    while (minimumLength > string.length) {
    string = “0″ + string;
    }

    // Return the result with a “0x” in front of the result.
    return “0x” + string;
    }
    }

    I hereby donate the copyrights over this work to you, Mims H. Wright.

  • aamp

    var k:int = 0x756c61626173;
    trace(getNumberAsHexString(k));

    procudes:

    Error: Error #1502: A script has executed for longer than the default timeout period of 15 seconds.

  • http://mimswright.com Mims H Wright

    @aamp
    Numbers larger than 0xFFFFFFFF exceed the memory size for a uint and will produce unexpected results.

  • http://onedayitwillmake.com mario gonzalez

    Or you could use:

    var num:uint = 123456789
    trace(num.toString(16))

  • laughing

    Yeah, so i tried using this and ran into an overflow bug I believe. Spent a good 15 minutes debugging it and then came back to see if anyone else had had the problem. I guess I learned you should read to the bottom.

    Good work Mario