asdg>> technotes>> octal, decimal, and hexadecimal numbers

This technote is also available in Korean here.
Not all ActionScript integer values are base-10 integers. Integers may also be represented in hexadecimal (base-16) and octal (base-8). Let's take a look at the difference.

A base-10 integer is an integer represented in our everyday number system. Remember the ones, tens, hundreds, and thousands columns from grade school? If not, here's a quick a refresher. Consider the following number:

723

The column on the far right is the ones column. The ones column represents how many "ones" are in our number: 3. The next column to the left is the tens column, which represents how many "tens" we have in our number: 2. The next column to the left is the hundreds column, which represents how many "hundreds" we have: 7. By looking at how many ones, tens, and hundreds our number represents, we can determine our number's total value: (7 x 100) + (2 x 10) + (3 x 1), or 723. Presto! About a month of primary-school math.

Now take a closer look at the ones, tens, and hundreds columns. Notice that as we move leftwards from column to column, each column's content represents the quantity we have of a growing power of ten. The number 3 in the ones column is multiplied by ten to the power of zero (100, or 1), which gives us 3. The number 2 in the tens column is multiplied by ten to the power of 1 (101, or 10) which gives us 20. The number 7 in the hundreds column is multiplied by ten to the power of 2 (102, or 100) which gives us 700. And so it goes to the right, with thousands next (103, or 1000), then ten thousands (104, or 10000), etc.

We say this counting system is "base-10" because there are 10 digits in the system (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). The number we use as the base of our growing exponent is, therefore, 10. We can, of course, change the base and still count quite happily. Consider what our number 723 becomes if we change the base to 8 (octal), a counting system with only 8 digits (0, 1, 2, 3, 4, 5, 6, 7). The right-most column is still the ones column because 80 is 1--anything to the power of 0 is 1. So our first column turns out to be 3 x 1. The second column is the eights column because 81 is 8. So our second column turns out to be 2 x 8. The third column is the sixty-fours column because 82 is 64. So our third column becomes 7 x 64. In base-8, then, the number 723 is: (7 x 64) + (2 x 8) + (3 x 1), which we represent as 467 in base-10.

By changing the base, we can count in any number system we like. We just happen to be familiar with base-10. Computers, for instance, work in base-2, or binary. ActionScript supports two bases besides base-10 as literal numbers: base-8 (octal) and base-16 (hexadecimal). Hexadecimal numbers may seem unusual because there are 16 digits in the counting system--6 more than you're accustomed to with ordinary base-10. The six additional digits are represented by letters: A (10), B (11), C (12), D (13), E (14) and F (15).

To indicate that a number is a base-8 literal integer, we put a leading zero in front of the number. For example, to represent 723 as a base-8 number-literal in ActionScript we'd use:

0723 // 467 in decimal (7*64 + 2*8 + 3*1)

To indicate that a number is a base-16 literal integer, we put 0x (or 0X) in front of the number, as in:

0x723 // 1827 in decimal (7*256 + 2*16 + 3*1)

We use plenty of hexadecimal numbers when manipulating the color of Movie Clips. However, most simple programs don't require anything but base-10 values. Still, even if you never use octal or hexadecimal-base numbers in your scripts, you at least now know why it's illegal to start a decimal literal with a zero. Placing a zero at the beginning of a number causes that number to be interpreted as an octal number. So be careful with your leading zeros, especially if you're converting strings to numbers. Be sure to trim any leading zeros off your strings before you convert them to base-10 numbers, or you're in for some weird results (see example 4-1 of asdg, on page 70 of the first edition).