Tagged with converter

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 , , , , , ,