为什么数字字符的ASCII值等于该值加"0"? [英] Why the ASCII value of a digit character is equal to the value plus '0'?
本文介绍了为什么数字字符的ASCII值等于该值加"0"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么要将数字的ASCII值转换为整数,我们需要这样做:
Why when we want to convert an ASCII value of a digit into an integer, we need to do:
value - '0'
?
反之,要将Integer转换为ASCII,我们需要做:
And the other way around, to convert Integer to ASCII, we need to do:
value + '0'
那是为什么?
推荐答案
因为C标准保证数字字符的整数值是连续的.
因此,可以从'1' - '0' == 1
,'2' - '0' == 2
等推断出您的公式确实有效.
Therefore '1' - '0' == 1
, '2' - '0' == 2
, etc. from which you can infer that your formulas really do work.
侧注:
- 由于标准保证了这一点,因此即使目标平台不使用ASCII,它也可以正常工作. 相反,如果该标准没有强制要求这样做(它不对字母值进行强制要求),则该技术将不可移植.它将取决于使用ASCII的目标系统.
- Since this is guaranteed by the standard, it works even if the target platform does not use ASCII.
- Conversely, if the standard did not mandate this (it does not do so with the values of the letters) then this technique would not be portable; it would be dependent on the target system using ASCII.
这篇关于为什么数字字符的ASCII值等于该值加"0"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文