1

I wanted to check that char is a digit by comparing its code with codes of 0 and 9, presuming that the digits codes are contiguous in code table:

(let ((char ?\e))
  (if (and (>= char ?\0) (<= char ?\9))
      (message "aaa")
    (message "bbb")))

But it doesn't work: this code returns "aaa", even though e is not a digit.

What is wrong?

1
  • nothing wrong, Probably your question is about ?e ?0 and ?9 and not the slashed version.
    – gigiair
    Commented Nov 17, 2021 at 11:28

1 Answer 1

8

Remove the backslashes before e, 0 and 9.

(let ((char ?e))
  (if (<= ?0 char ?9)
      (message "It's a digit")
    (message "It's not a digit")))

With the backslashes:

  • \e is the escape char - ASCII 27.
  • \0 is the null char - ASCII 0.
  • \9 isn't special, so it's the same as 9 - ASCII 57.
  • 27 is >= 0 and <= 57.
3
  • "\0 is null char ascii 0" - how to use ASCII 48 = "0" instead of ASCII 0?
    – user4035
    Commented Nov 17, 2021 at 11:35
  • 4
    This answer tells and shows you exactly how to do that.
    – phils
    Commented Nov 17, 2021 at 12:03
  • 3
    See also C-h i g (elisp)Basic Char Syntax and (elisp)General Escape Syntax. Note that ?\0 is being read as an octal value which is also why ?\9 is not treated the same way (as only the digits 0-7 are valid in octal). You can specify octal character codes up to ?\777 with this syntax.
    – phils
    Commented Nov 17, 2021 at 13:29

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.