Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I get the ASCII value of the alphabet?

For example, 97 for a?

share|improve this question

5 Answers

up vote 23 down vote accepted

Define these two functions (usually available in other languages):

chr() {
  [ "$1" -lt 256 ] || return 1
  printf "\\$(printf '%03o' "$1")"
}

ord() {
  LC_CTYPE=C printf '%d' "'$1"
}

Usage:

chr 65
A

ord A
65
share|improve this answer
That's perfect!! – xmpirate 2 days ago
@dmsk80: +1. For others like me who think they spot a typo: "'A" is correct whereas if you use "A" it will say : A: invalid number . It seems it's done on printf side (ie, in the shell, "'A" is indeed 2 chars, a ' and a A. Those are passed to printf. And in the printf context, it is converted to the ascii value of A, (and is finally printed as a decimal thanks to the '%d'. Use 'Ox%x' to show it in hexa or '0%o' to have it in octal)) – Olivier Dulac 2 days ago

You can see the entire set with:

$ man ascii

You'll get tables in octal, hex, and decimal.

share|improve this answer
There's also an ascii package for debian-based distros, but (at least now) the question is tagged as bash, so these wouldn't help the OP. In fact, it's installed on my system and all I get from man ascii is its man page. – Joe yesterday

If you want to extend it to UTF-8 characters:

$ perl -CA -le 'print ord shift' 😈
128520

$ perl -CS -le 'print chr shift' 128520
😈

With bash, ksh or zsh builtins:

$ printf "\U$(printf %08x 128520)\n"
😈
share|improve this answer

This works well,

echo "A" | tr -d "\n" | od -An -t uC
share|improve this answer
Can you maybe add a small explanation? – Bernhard 2 days ago
tr to remove "\n" (new line ) from the input. od is used to -t dC is to print in decimal character. – Saravanan 2 days ago
echo -n suppresses trailing newline eliminating the need for tr -d "\n" – Gowtham 2 days ago
@Gowtham, only with some implementations of echo, not in Unix compliant echos for instance. printf %s A would be the portable one. – Stephane Chazelas yesterday

I'm going for the simple (and elegant?) Bash solution:

for i in {a..z}; do echo $(printf "%s %d" "$i" "'$i"); done

For in a script you can use the following:

CharValue="A"
AscValue=`printf "%d" "'$CharValue"

Notice the single quote before the CharValue. It is obligated...

share|improve this answer
1  
How is your answer different from dsmsk80's answer? – Bernhard 2 days ago
My interpretation of the question is "how to get the ASCII values for the values of the alphabet". Not how to define a function to retrieve the ASCII value for one character. So my first answer is a short one-line command to get the ASCII values for the alphabet. – phulstaert yesterday
I get your point, but I still think the bottom line of both answer is printf "%d". – Bernhard yesterday
I agree this is a crucial part of the process to get to the result, yet i didn't wanted to make the assumption that xmpirate knew about the "for i in" and the use of a range. If he wanted a list, this could be a real time-saver ;-). Also, future readers might find my additions helpful. – phulstaert yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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