Node:Nondecimal-numbers, Next:, Previous:Scalar Constants, Up:Constants



Octal and Hexadecimal Numbers

In awk, all numbers are in decimal; i.e., base 10. Many other programming languages allow you to specify numbers in other bases, often octal (base 8) and hexadecimal (base 16). In octal, the numbers go 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, etc. Just as 11, in decimal, is 1 times 10 plus 1, so 11, in octal, is 1 times 8, plus 1. This equals 9 in decimal. In hexadecimal, there are 16 digits. Since the everyday decimal number system only has ten digits (0-9), the letters a through f are used to represent the rest. (Case in the letters is usually irrelevant; hexadecimal a and A have the same value.) Thus, 11, in hexadecimal, is 1 times 16 plus 1, which equals 17 in decimal.

Just by looking at plain 11, you can't tell what base it's in. So, in C, C++, and other languages derived from C, there is a special notation to help signify the base. Octal numbers start with a leading 0, and hexadecimal numbers start with a leading 0x or 0X:

11
Decimal value 11.
011
Octal 11, decimal value 9.
0x11
Hexadecimal 11, decimal value 17.

This example shows the difference:

$ gawk 'BEGIN { printf "%d, %d, %d\n", 011, 11, 0x11 }'
-| 9, 11, 17

Being able to use octal and hexadecimal constants in your programs is most useful when working with data that cannot be represented conveniently as characters or as regular numbers, such as binary data of various sorts.

gawk allows the use of octal and hexadecimal constants in your program text. However, such numbers in the input data are not treated differently; doing so by default would break old programs. (If you really need to do this, use the --non-decimal-data command-line option; see Allowing Nondecimal Input Data.) If you have octal or hexadecimal data, you can use the strtonum function (see String Manipulation Functions) to convert the data into a number. Most of the time, you will want to use octal or hexadecimal constants when working with the built-in bit manipulation functions; see Using gawk's Bit Manipulation Functions, for more information.

Unlike some early C implementations, 8 and 9 are not valid in octal constants; e.g., gawk treats 018 as decimal 18:

$ gawk 'BEGIN { print "021 is", 021 ; print 018 }'
-| 021 is 17
-| 18

Octal and hexadecimal source code constants are a gawk extension. If gawk is in compatibility mode (see Command-Line Options), they are not available.

Advanced Notes: A Constant's Base Does Not Affect Its Value

Once a numeric constant has been converted internally into a number, gawk no longer remembers what the original form of the constant was; the internal value is always used. This has particular consequences for conversion of numbers to strings:

$ gawk 'BEGIN { printf "0x11 is <%s>\n", 0x11 }'
-| 0x11 is <17>