string - '0' 做什么(字符串是一个字符) [英] what does string - '0' do (string is a char)
问题描述
这是做什么的
while(*string) {
i = (i << 3) + (i<<1) + (*string -'0');
string++;
}
*string -'0'
the *string -'0'
它会删除字符值还是什么?
does it remove the character value or something?
推荐答案
这会从 string
指向的字符中减去字符 '0'
的 ASCII 码.所以,'0'
- '0'
给你 0
等等,'9'
- '0'
为您提供 9
.
This subtracts from the character to which string
is pointing the ASCII code of the character '0'
. So, '0'
- '0'
gives you 0
and so on and '9'
- '0'
gives you 9
.
整个循环基本上是手动"计算字符串string
指向的十进制整数的数值.
The entire loop is basically calculating "manually" the numerical value of the decimal integer in the string string
points to.
那是因为 i <<3
等价于 i * 8
和 i <<1
等价于 i * 2
并且 (i << 3) + (i<<1)
等价于 i *8 + i * 2
或 i * 10
.
That's because i << 3
is equivalent to i * 8
and i << 1
is equivalent to i * 2
and (i << 3) + (i<<1)
is equivalent to i * 8 + i * 2
or i * 10
.
这篇关于string - '0' 做什么(字符串是一个字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!