# How to convert English letter to its alphabet index, and to its ASCII/Unicode index?

I need to convert a letter to its index in the alphabet, and to its ASCII / Unicode index. And would like to have more than one way of achieving each of the cases (because I remember there are more than one), if possible.

First I wanted to convert a letter to its alphabet index (I remember some of the users here showed me how to do the conversion a while ago [either in the chat or in the comment section to one of the questions] but I didn't copy examples and forgot how to do it [I can't seem to find anything in the archives]), but then I decided to add ASCII- / Unicode-related index of a letter in the mix since this must be a quite similar procedure.

I recall something like '\a to reference the character a but can't seem to make it work or remember exactly what it is used for. I'll be reading manuals shortly but in the meanwhile it made sense to ask the question since it may be faster.

Thank you.

• The syntax is <backtick><character> to get the character code of the letter. For the alphabet index you can just subtract the index of a (or A respectively). – siracusa Aug 1 at 3:17

The TeXBook says:

A number in TeX's language can begin with a ', in which case it is regarded as octal, or with a ", when it is regarded as hexadecimal. Thus, \char'142 and \char"62 are equivalent to \char98.

and

The token 12 (left quote), when followed by any character token or by any control sequence token whose name is a single character, stands for TeX's internal code for the character in question. For example, \charb and \char\b are also equivalent to \char98.

And these internal codes are (from Appendix C of The TeXBook):

(octal numbers are represented in italics, and hexadecimal numbers in typewriter font) which is the same as the ASCII table.

So for TeX all of 98, '142, "62, and b are valid and represent the same number.

The TeXBook also tells you what the \number primitive does:

• \number. When TeX expands \number, it reads the number that follows (expanding tokens as it goes); the final expansion consists of the decimal representation of that number, preceded by '-' if negative.

So you can add both and have what you want! In \numberb, \number reads the number b and expands to its decimal representation, 98, which is the ASCII code for b.

If you want the alphabetical index of such letter, you can do as siracusa suggested and subtract from the index of a (or A, if dealing with uppercase letters):

\the\numexprz-a+1\relax % prints 26


(you need to add 1 because a-a would result in zero). Here you don't need number because \numexpr already knows that z and a are numbers; you just need \the to expand \numexpr.

The same goes for Unicode characters. \number₢ (chosen at random) prints 8354, which is the decimal representation of the unicode point U+20A2. Of course you need XeTeX or LuaTeX to use these.

• Honorable mention: \lccode and \uccode. – bp2017 Aug 2 at 6:54
• @bp2017 Well, yes, those can work too. However note that you can (but shouldn't, obviously) set \lccodeb=a, then \the\lccodeb will be 97, not 98. Also \lccodeb is (usually) equal \lccodeB, whereas \numberb and \numberB are different. Also, the \lccode of non-letter characters (\lccode!, for example) is zero, not the ASCII index. The same goes for \uccode. – Phelype Oleinik Aug 2 at 18:28
• There's also \@arabic. (It can take a letter, as CHAR, and expand to digit.) – bp2017 Aug 3 at 12:39
• @bp2017 Yes because \@arabic{<stuff>} expands to \number <stuff>. And for TeX ‌CHAR isn't a letter (although it looks like one), but a number. That's why \number (and \@arabic`) works. – Phelype Oleinik Aug 3 at 13:07