An excellent post to our mailing list over the weekend by our member Chip Brown on the nature of Radix's and their use in ActionScript.
In ActionScript, the value of anything not inside of quotes is assumed to be an identifier (name for a variable, function, class, etc) and anything starting with a number is assumed to be a number. (Hence the prohibition of starting variable names with a number.) So the zero tells AS that we're dealing with a number, the x tells AS that it's a hex-based number, not a base ten. The rest is the actual number. So any input needs to start with 0x, but output doesn't need it.
For those who are not familiar with Radix (or Base), the radix is the number of units per digit in a visual representation of a number. What this means is how many numbers until you reach a second digit. Normally we deal with numbers in zero to nine, above that we need additional numbers. In a hexadecimal number the base is 16, so we have numbers zero through F, where F is equal to 15 in "normal" base 10 terms. Computers use Hex numbers because one byte is eight bits, which is 256 possibilities (0 to 255), and a hex number displays all possible values in exactly two digits. (00 is zero, FF is two hundred and fifty six). But you can have a number with any radix, some of which are better than others, depending on the purposes. ActionScript supports radix 2 to 36.
One example of another use, if you output a number as radix 2 you'll have the binary code for that number!
Remember, as soon as you put a number into a computer it's binary (base 2), then when it returns the value it has to convert it back to base ten by default (and then to ascii and finally pixels)
In AS2 there is a function parseInt(expression:String, [radix:Number]) : Number which converts a string to an radix (default 10)
I'm not sure if AS3 has an equivalent or not.
(EDITOR'S NOTE: The AS3 Equivalent is int.toString(radix). Read more at this link)
Comments
Nice summary on Radix, Michael!
Yes, parseInt has changed from AS2 to AS3, as migration docs say:
And parseInt is still usable in AS3 / Flex 3, as well as in Flex 4.
And, as you wrote, int.toString(radix) works too :)
Thanks,
Rost