1

As it is well known, characters in emacs are indistinguishable from the integer number representing them, so much so that the form (eq ?A 65) returns t.

Nevertheless, there are situations in which one might want to start out with an integer, such as 9, and print the associated character representation, such as ?\C-i.

For example, when one evaluates an expression such as 9 using the function eval-last-sexp, the minibuffer shows the result of this evaluation, among other things, as (#o11, #x9, ?\C-i), indicating that the number 9 may be alternatively represented as #o11, #x9, ?\C-i.

Specifically I'd like a command, say char-representation-of-number which gives the third representation above, so that

(char-representation-of-number 1) ⇒ "?\C-a"

(char-representation-of-number 9) ⇒ "?\C-i"

(char-representation-of-number 65) ⇒ "?A"

Is there a native command in emacs allowing one to do so?

PS: Note that the command single-key-description (see How to get the string representation of a keymap event?) does approximately what I want, but not quite. For example:

(single-key-description 9) ⇒ "TAB" ;; (I wish this was "?\C-i")

(single-key-description 8) ⇒ "C-h" ;; I wish this was "?\C-h")

(single-key-description 65) ⇒ "A" ;; I wish this was "?A")

In all of these cases, evaluation of the output of single-key-description using eval-last-sexp doesn't give you the input number back.

6
  • Can you say why you want the form "?\..." instead of what single-key-description gives you? (This info isn't needed for your question, but are you sure you need the form you request?)
    – Drew
    Jun 10, 2023 at 23:16
  • Also, if you want to be able to evaluate the result you're looking for and get back what you started with, then you certainly don't want some code to give you the string "?\C-i"; you instead want some code to give you the character ?\C-i, i.e., the number 9. Evaluating the string with eval-last-sexp won't give you the character. If you want to use eval(-last-sexp) then you need to evaluate the char itself (to give the char itself).
    – Drew
    Jun 10, 2023 at 23:19
  • This sounds like an X-Y question. What is it that you're really trying to do? Why do you think you need to use strings describing chars as input somewhere (as opposed to just using the chars)?
    – Drew
    Jun 10, 2023 at 23:24
  • The thing to keep in mind is that characters ARE just numbers (certain kinds of numbers). There's no way to distinguish them because they are the same thing (as you state at the outset). So it's unclear what kind of distinction you're aiming to draw/produce.
    – Drew
    Jun 10, 2023 at 23:27
  • Thanks for your comments @Drew, but with due respect I think you are giving me what I would call a Y-X answer, namely you are trying go guess why am I asking the question instead of addressing the question itself. Let's just say I got curious to understanding the relationship between integers and characters.
    – Ruy
    Jun 10, 2023 at 23:39

1 Answer 1

2

Found it! The answer is simply prin1-char.

Here is the help text description of it:

prin1-char is a compiled Lisp function in ‘elisp-mode.el’.

(prin1-char CHAR)

Return a string representing CHAR as a character rather than as an integer. If CHAR is not a character, return nil.

1
  • To quasi-quote you, this doesn't answer your request, because "evaluation of the output of prin1-char using eval-last-sexp doesn't give you the input number back." The output of prin1-char is a string, and evaluating that doesn't give you the char/number you started with. I tried to point out this confusion in a previous comment on your question. So if this gives you what you were really looking for then your question doesn't say what you were really looking for.
    – Drew
    Jun 11, 2023 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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