Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the character code using only bash, without xdd? [closed]

Tags:

linux

bash

unix

No use perl, python, and similar.

like image 223
WhitePavel Avatar asked Dec 01 '11 09:12

WhitePavel


1 Answers

Use printf, a bash builtin: http://mywiki.wooledge.org/BashFAQ/071

If you want the ASCII code of a character in hex:

function ord {
  printf %x "'$1"
}

ord A # 41
ord 0 # 30

The use of the single leading quote is explained here: http://pubs.opengroup.org/onlinepubs/009695399/utilities/printf.html

If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote.

like image 115
David Hu Avatar answered Sep 30 '22 00:09

David Hu