ASCII, and by extension nearly every other text encoding in common use, uses the codes 48 (0x30) through 57 (0x39) to represent digits 0-9. If you subtract 48 from the character code, you get the digit value...and if you add 48 to the digit value, you get the character code.
(You can usually use '0'
as an alias for 48, and it usually makes the intention clearer.)
You could also and
the code with 0x0f
to get the digit value, if you're absolutely sure it represents a digit. or
ing with 0x30
will turn a number from 0-9 back into a character code. Subtraction and addition have the semantic benefit of symmetry, though; you use the same value, just a little differently, to convert both ways.
(If you're not sure your register contains a value between 0 and 9 inclusive, by the way, basically any math to convert it to an ASCII digit will mess up. Similarly, converting to a digit value will mess up for any character codes that don't represent an ASCII digit. Know that the value is in the right range before "converting" it, or you will see wackiness.)