1

awk hex to decimal result is incorrect, not equal with bash/python

echo 0x06375FDFAE88312A |awk --non-decimal-data '{printf "%d\n",$1}'

or

echo 0x06375FDFAE88312A |awk '{printf "%d\n",strtonum($1)}'

the result is 447932102257160448, but with python the result is 447932102257160490

python -c "print int('0x06375FDFAE88312A', 16)"
Applee
  • 23
  • 5

1 Answers1

3

You need to use --bignum option, as this answer suggests. (Supported in gawk since version 4.1).

echo 0x06375FDFAE88312A |awk --bignum '{printf "%d\n",strtonum($1)}'

echo 0x06375FDFAE88312A |awk --bignum --non-decimal-data '{printf "%d\n",$1}'

The problem is that AWK typically uses double floating point number to represent numbers by default, so there is a limit on how many exact digits can be stored that way.

Andriy Makukha
  • 7,580
  • 1
  • 38
  • 49