In as3 it might be a simple and convenient way to store all sort of variables as String. When you have to store or transfer intergers (int or uint) it’s very easy to keep them in hexadecimal values. For example an array of integer can be stored in a comma separated string or in a string of values of the same length. For the first case everything is trivial. In the second case we have to take care when we serialize the integer values to prefix them with zeros so all of them will have the same length.
All we have to do to transform an integer to a hex string is to use toString() method.
Then to do get the int from a string we can use the parseInt global function:
If we want to have the same dimension for all of them we should use some helper functions to prefix the strings with zeros when necessary. The following example transforms the integers in strings of length of 3(min 000, max FFF).
* transform and int to a hex fixed width string prefixing it with zeros if required
static private function intTo3Str(i:int):String
return extendStr((i).toString(16).toUpperCase(), 3 );
* extends a string to a specified length by adding extra characters before or after
static private function extendStr(str:String, len:int, c:String = '0', prefix:Boolean = true):String
var extraLength:int = len - str.length;
var extra:String = '';
for (var i:int = 0; i < extraLength; i++)
extra += c;
if (prefix) return extra + str;
else return str + extra;
The same code can be used for colors but 6 should be used instead of 3, and preceding '#' or '0x' could be also taken in consideration.