awker
awker

Reputation: 13

AWK, printf format specifier %x has problems with negative values

It seems AWK has problems with the unsigned hex format specifier:

echo 0x80000000 | awk '{printf("0x%08x\n", $1)}'

gives back: 0x7fffffff

Is this a known problem with awk?

Thanks!

Upvotes: 1

Views: 1175

Answers (2)

jcristovao
jcristovao

Reputation: 554

The problem is that awk only converts input parameters to numbers automatically if they are decimal. But this should work:

echo 0x80000000 | awk '{printf("0x%08x\n", strtonum($1))}'

It's all explained in here, in the strtonum section: http://www.gnu.org/manual/gawk/html_node/String-Functions.html#String-Functions

Upvotes: 1

falstro
falstro

Reputation: 35667

Not seeing it here, although I wasn't able to use the hex input as you are, but converted to decimal was no problem.

$ echo 2147483648 | awk '{printf("0x%08x\n", $1)}'
0x80000000

If you care to enlighten us what platform you're on (this was GNU awk 3.1.5), we might be able to help you more.

Upvotes: 0

Related Questions