I need to print the ASCII value of the given character in awk only.
Below code gives 0
as output:
echo a | awk '{ printf("%d \n",$1); }'
Using only basic awk (not even gawk, so the below should work on all BSD and Linux variants):
$ echo a | awk 'BEGIN{for(n=0;n<256;n++)ord[sprintf("%c",n)]=n}{print ord[$1]}'
97
Here's the opposite direction (for completeness):
$ echo 97 | awk 'BEGIN{for(n=0;n<256;n++)chr[n]=sprintf("%c",n)}{print chr[$1]}'
a
Basic premise is to use a lookup table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With