How to print ASCII value of a character using basic awk only

前端 未结 3 850
温柔的废话
温柔的废话 2020-12-09 10:09

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)         


        
3条回答
  •  情话喂你
    2020-12-09 10:55

    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 }
    

提交回复
热议问题