Converting characters to integers

If you have the character #\7 and you want the integer 7, you might be tempted to use (parse-integer (string char)) or even this ASCII-oriented technique:

(- (char-int char) (char-int #\0))

While the former is specified to give the right answer, the latter will only work by coincidence. The spec does discuss character ordering, but it makes no guarantees about the values returned by char-int or char-code.

What to use, then? digit-char-p not only returns a true value if its first argument represents a digit, the true value it returns is the integer value of that digit:

* (digit-char-p #\7)

It also works with other radixes:

* (digit-char-p #\a 16)

If the character is not a digit, digit-char-p returns nil.