Recipe 4.2.
Converting Between Different Number Systems
Problem
You
want to convert a number
between different bases (such as decimal, binary, hexadecimal,
etc.).
Solution
Use the
parseInt( ) function with the radix
parameter (the radix is the
number's base) to convert a string to a decimal representation. Use
the toString( ) method of a Number, uint, or
int object with the radix parameter to convert a decimal
number to a string representation of the value in another base.
Discussion
No matter how you set a number value in
ActionScript, the result is always retrieved as a decimal (base-10)
number:
// Create a Color object
var pink:ColorTransform = new ColorTransform( );
// Set the RGB value as a hexadecimal
pink.rgb = 0xF612AB;
// This displays the value as decimal: 16126635
trace(pink.rgb);
However, if you want to output a value in a
different base, you can use toString( radix
) for a Number, uint, or int object to
convert any number value to a string representing that number in
the specified base.
These two examples convert numeric literals to
uint objects and output the string representations in base-2
(binary) and base-16 (hexadecimal) format.
// The radix is 2, so output as binary
trace(new uint(51).toString(2)); // Displays: 110011
// The radix is 16, so output as hex
trace(new uint(25).toString(16)); // Displays: 19
When using the toString( ) method with a
variable that contains a numeric literal value, Flash automatically
creates a new Number, uint, or int object
before calling the toString( ) method. Although it's not
typically the best practice, it is not technically wrong, and in
most applications the differences are negligible. This example
assigns a primitive number to a variable and calls the toString(
) method to output the value in hexadecimal:
var quantity:Number = 164;
trace(quantity.toString(16)); // Displays: a4
|
The results from these examples are not numeric
literals, but rather strings, such as 110011, 19, and A4.
|
|
The following example sets the RGB value of a
ColorTransform object,
calls toString( ) on the result to display the value as a
hexadecimal (as it had been input, although the alpha digits are
converted to lowercase, and the result is a string, not a
number):
// Create a Color object
var pink:Color = new ColorTransform( );
// Set the RGB value as a hexadecimal
pink.rgb = 0xF612AB;
trace(pink.rgb.toString(16)); // Displays: f612ab
The valid range for the radix
parameter of the toString( ) method is from 2 to 36. If you
call toString( ) with no radix parameter or
an invalid value, decimal format (base-10) is assumed.
You can achieve the inverse of the toString(
) process using the parseInt( ) function with the
radix parameter. The parseInt( ) function
takes a string value and returns a number. This is useful if you
want to work with base inputs other than 10.
These examples parse the numbers from the string
in base-2 (binary), base-16 (hexadecimal), and base-10,
respectively (note that the result is always a decimal):
trace(parseInt("110011", 2)); // Displays: 51
trace(parseInt("19", 16)); // Displays: 25
trace(parseInt("17", 10)); // Displays: 17
If omitted, the radix is assumed
to be 10, unless the string starts with 0x, 0X,
or 0, in which case hexadecimal or octal is assumed:
trace(parseInt("0x12")); // The radix is implicitly 16. Displays: 18
trace(parseInt("017")); // The radix is implicitly 8. Displays: 15
An explicit radix overrides an
implicit one. In the next example, the result is 0, not 12. When
the number is treated base-10, conversion stops when a non-numeric
characterthe xis encountered:
// The number is treated as a decimal, not a hexadecimal number
trace(parseInt("0x12", 10)); // Displays: 0 (not 12 or 18)
Here, although the leading zero doesn't prevent
the remainder digits from being interpreted, it is treated as a
decimal number, not an octal number:
// The number is treated as a decimal, not an octal number
trace(parseInt("017", 10)); // Displays: 17 (not 15)
Don't forget to include 0, 0x,
or an explicit radix. The following interprets the string as a
decimal and returns NaN (not a number) because "A" can't
be converted to an integer:
trace(parseInt("A9FC9C")); // NaN
|