I'm parsing an ntpq output to pass its contents to our database via console utility. The first element is a one character substring containing status of the remote. It can be "*", "+", "o", "#" etc. I'd better put it to the DB as a number, so it can be easily held by stored procedures, but I have no idea, how to convert a character into it's ASCII representation in bash.


Put your character into the variable A.

printf '%d\n' "'$A"

Some examples:

$ A="*"; printf '%d\n' "'$A"
$ A="+"; printf '%d\n' "'$A"
$ A="#"; printf '%d\n' "'$A"
$ A="o"; printf '%d\n' "'$A"
  • 3
    This probably goes without saying, but the variable itself isn't required: e.g., printf '%d\n' "'*". May 14 '18 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.