tags:

views:

174

answers:

3

I need to print the ASCII value of the given charecter in awk only.

the below gives 0 as output.

echo a | awk '{ printf("%d \n",$1); }'

Help please.

A: 

See if this thread helps you: I love google

David V.
A: 

It seems this is not a trivial problem. I found this approach using a lookup array, which should work for A-Z at least:

 BEGIN { convert="ABCDEFGHIJKLMNOPQRSTUVWXYZ" } 
       { num=index(convert,substr($0,2,1))+64; print num }
Martin Wickman
for this solution to work i need to create lookup array for all the characters in the ASCII table in the order.
Venkataramesh Kommoju
Yes, that is correct. It's only 127 values though, so it's not that big a deal.
Martin Wickman
+3  A: 

see the awk manual for ordinal functions you can use. But since you are using awk, you should be on some version of shell, eg bash. so why not use the shell?

$ printf "%d" "'a"
97
ghostdog74
it will be part of some existing awk code.
Venkataramesh Kommoju