gawk: Nondecimal-numbers

1 
1 6.1.1.2 Octal and Hexadecimal Numbers
1 .....................................
1 
1 In 'awk', all numbers are in decimal (i.e., base 10).  Many other
1 programming languages allow you to specify numbers in other bases, often
1 octal (base 8) and hexadecimal (base 16).  In octal, the numbers go 0,
1 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, and so on.  Just as '11' in decimal is
1 1 times 10 plus 1, so '11' in octal is 1 times 8 plus 1.  This equals 9
1 in decimal.  In hexadecimal, there are 16 digits.  Because the everyday
1 decimal number system only has ten digits ('0'-'9'), the letters 'a'
1 through 'f' represent the rest.  (Case in the letters is usually
1 irrelevant; hexadecimal 'a' and 'A' have the same value.)  Thus, '11' in
1 hexadecimal is 1 times 16 plus 1, which equals 17 in decimal.
1 
1    Just by looking at plain '11', you can't tell what base it's in.  So,
1 in C, C++, and other languages derived from C, there is a special
1 notation to signify the base.  Octal numbers start with a leading '0',
1 and hexadecimal numbers start with a leading '0x' or '0X':
1 
1 '11'
1      Decimal value 11
1 
1 '011'
1      Octal 11, decimal value 9
1 
1 '0x11'
1      Hexadecimal 11, decimal value 17
1 
1    This example shows the difference:
1 
1      $ gawk 'BEGIN { printf "%d, %d, %d\n", 011, 11, 0x11 }'
1      -| 9, 11, 17
1 
1    Being able to use octal and hexadecimal constants in your programs is
1 most useful when working with data that cannot be represented
1 conveniently as characters or as regular numbers, such as binary data of
1 various sorts.
1 
1    'gawk' allows the use of octal and hexadecimal constants in your
1 program text.  However, such numbers in the input data are not treated
1 differently; doing so by default would break old programs.  (If you
1 really need to do this, use the '--non-decimal-data' command-line
1 option; ⇒Nondecimal Data.)  If you have octal or hexadecimal
1 data, you can use the 'strtonum()' function (⇒String Functions)
1 to convert the data into a number.  Most of the time, you will want to
1 use octal or hexadecimal constants when working with the built-in
1 bit-manipulation functions; see ⇒Bitwise Functions for more
1 information.
1 
1    Unlike in some early C implementations, '8' and '9' are not valid in
1 octal constants.  For example, 'gawk' treats '018' as decimal 18:
1 
1      $ gawk 'BEGIN { print "021 is", 021 ; print 018 }'
1      -| 021 is 17
1      -| 18
1 
1    Octal and hexadecimal source code constants are a 'gawk' extension.
1 If 'gawk' is in compatibility mode (⇒Options), they are not
1 available.
1 
1               A Constant's Base Does Not Affect Its Value
1 
1    Once a numeric constant has been converted internally into a number,
1 'gawk' no longer remembers what the original form of the constant was;
1 the internal value is always used.  This has particular consequences for
1 conversion of numbers to strings:
1 
1      $ gawk 'BEGIN { printf "0x11 is <%s>\n", 0x11 }'
1      -| 0x11 is <17>
1