One trick to get awk to print out any arbitrary byte, regardless of whether your locale is UTF8 or POSIX or C, is to add a large multiple of 256 to the unsigned byte ordinance value to bring the new number above 0x10FFFF
, the limit of Unicode 14 spec.
Here's a demo of how to access arbitrary bytes in gawk byte mode to print out UTF8-encoded characters. The same method can also be used in gawk unicode mode to access any byte:
gawk -e 'BEGIN { printf("%c",50000) }' | od -baxco -t dC
0000000 354 215 220
? 8d 90
8dec 0090
썐 ** **
106754 000220
-20-115-112
0000003
% gawk -b -e 'BEGIN { printf("%c%c%c",
(-20)+8^8,
(-115)+8^8,
(-112)+8^8) }' | od -baxco -t dC
0000000 354 215 220
? 8d 90
8dec 0090
썐 ** **
106754 000220
-20-115-112
0000003
% gawk -e 'BEGIN { printf("%c%c%c%c",\
\
0xAB+8^8, 0xBA+8^8, \
0xCA+8^8, 0xFE+8^8) }' \
| god --endian=big -baxco -t dCxI
0000000 253 272 312 376
+ : J ~
abba cafe
? ? ? ?
125672 145376
-85 -70 -54 -2
abbacafe
0000004
This method works regardless of your locale settings.
For mawk-1, mawk2-beta, and nawk, you can also subtract 256 from the unsigned byte value and do printf("%c")
using negative numbers. gawk used to also allow that, but recent versions might have disabled it.