Search code examples
unicodeelispchrord

How to get code point of a character in elisp (and other way too)?


I was very surprised not to be able to find this in the elisp manual or SO. I just want the equivalent of many languages' chr() and ord() or similar: convert between actual characters and their (unicode) code point values.

Emacs Lisp: getting ascii value of character explains that to elisp, a char just is its code-point. But what if I need the representation of that char~int as a series of ASCII decimal digits?

For example, if I wanted to generate in a buffer, a readable table showing the equivalences?

Thanks!


Solution

  • As you've already noted, characters are integers.

    (eq ?A 65)
    

    For example, if I wanted to generate in a buffer

    Either of the following inserts the character A into the buffer:

    (insert ?A)
    (insert 65)
    

    If you need to deal with strings, characters can be converted to strings:

    (char-to-string ?A)
    (char-to-string 65)
    (format "%c" 65)
    "A"
    

    vs

    (number-to-string 65)
    (format "%d" 65)
    "65"