2

The ASCII character range is from 0 to 127, and within that range, awk's printf with the %c format specifier outputs one byte of data:

$ awk 'BEGIN{printf "%c", 97}'
a

$ awk 'BEGIN{printf "%c", 127}' | xxd
00000000: 7f

$ awk 'BEGIN{printf "%c", 127}' | xxd -b
00000000: 01111111

But for values greater than 127, it will print out multiple bytes:

$ awk 'BEGIN{printf "%c", 128}' | xxd
00000000: c280

$ awk 'BEGIN{printf "%c", 128}' | xxd -b
00000000: 11000010 10000000

What is the significance of 0xc280, and why does awk output that character instead of 0x80?

3

This is UTF-8 encoding. 11000010 starts a two-byte sequence (the first two bits set followed by a clear bit), and the significant bits are 00010000000 (the last five bits of the first byte, and the last six bits of the second byte), which is 128.

AWK is outputting this because your locale is set to use UTF-8; you can switch to a non-UTF-8 locale to see the difference:

$ LC_ALL=C awk 'BEGIN{printf "%c", 128}' | xxd -b
00000000: 10000000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.