DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 5.2 Converting Between Different Number Systems

5.2.1 Problem

You want to convert a number between different bases (decimal, binary, hexadecimal, etc.).

5.2.2 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 Number.toString( ) method with the radix parameter to convert a decimal number to a string representation of the value in another base.

5.2.3 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.
myColor = new Color(this);

// Set the RGB value as a hexadecimal.
myColor.setRGB(0xF612AB);

// This displays the value as decimal: 16126635
trace(myColor.getRGB(  ));

However, if you want to output a value in a different base, you can use Number.toString(radix) to convert any number value to a string representing that number in the specified base.

These two examples convert numeric literals to Number 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 Number(51).toString(2));  // Displays: "110011"
// The radix is 16, so output as hex.
trace(new Number(25).toString(16)); // Displays: "19"

This example assigns a primitive number to a variable and calls the toString( ) method to output the value in hexadecimal:

myNum = 164;
trace(myNum.toString(16)); // Displays: "A4"

Note that the results from these examples are not numeric literals, but rather strings, such as "110011", "19", and "A4".

This example sets the RGB value of a Color object, then 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):

myColor = new Color(this);
myColor.setRGB(0xF612AB);
trace(myColor.getRGB(  ).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 inputs of bases 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 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 as base-10, conversion stops when a nonnumeric character—the x—is 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 remaining 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)

Due to a deviation from the ECMA-262 standard, ActionScript gets confused by the "0x" that prefixes a hexadecimal number if you specify an explicit radix of 16. Therefore, if you specify an explicit radix, don't prefix the number with "0x". For example, although the following should, according to the ECMA-262 standard, return the decimal equivalent of 0xA9FC9C, it returns 0 instead:

trace (parseInt("0xA9FC9C", 16));   // Displays: 0

Either of these will work as expected:

trace (parseInt("0xA9FC9C"));     // Displays: 11140252 (implicit radix)
trace (parseInt("A9FC9C", 16));   // Displays: 11140252 (explicit radix)

Conversely, don't forget to include either "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
    [ Team LiB ] Previous Section Next Section