Why when we want to convert an ASCII value of a digit into an integer, we need to do:
value - '0'
?
And the other way around, to convert Integer to ASCII, we need to do:
value + '0'
Why is that?
Character form of a decimal digit In ASCII, the number character is not the same as the actual number value. For example, the ASCII value 011 0100 will print the character '4', the binary value is actually equal to the decimal number 52. Therefore ASCII cannot be used for arithmetic.
ASCII (American Standard Code for Information Interchange) is the most common character encoding format for text data in computers and on the internet. In standard ASCII-encoded data, there are unique values for 128 alphabetic, numeric or special additional characters and control codes.
Because the integral values of the digit characters are guaranteed by the C standard to be consecutive.
Therefore '1' - '0' == 1
, '2' - '0' == 2
, etc. from which you can infer that your formulas really do work.
Sidenotes:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With