简体   繁体   English

在C中有Atoi函数吗?

[英]Atoi function in C?

int atoi(char* s)
{
   int i,n;
   n=0;
   for (i=0; (s[i]>='0') && (s[i]<='9'); i++)
       n = 10 * n + (s[i] - '0');

   return n;
}

In this code what is s[i]-'0' doing? 在这段代码中s [i]-'0'在做什么? Can anyone please explain the detailed working of this function? 谁能解释这个功能的详细工作吗?

Have a look at the table in the link below- 在下面的链接中查看表格-

http://www.asciitable.com/ http://www.asciitable.com/

The table is called ASCII Table and it is one of the character-encoding schemes used to represent characters in the binary world. 该表称为ASCII表,它是用来表示二进制世界中字符的字符编码方案之一。

You can see the decimal numbers 0-9 are represented by numbers 48-57 in the ASCII table. 您可以在ASCII表中看到十进制数字0-9由数字48-57表示。 All digits(0-9) are stored as characters. 所有数字(0-9)都存储为字符。

If your computer stores 48 for decimal number 0, 49 for decimal number 1, 50 for decimal number 2 and so-on. 如果您的计算机存储的十进制数字0为48,十进制数字1为49,十进制数字2为50,依此类推。 Then, to convert a ASCII number into decimal number, you just need to subtract 48 from ASCII number. 然后,要将ASCII数字转换为十进制数字,您只需要从ASCII数字中减去48。 For example, 例如,

48 - 48 => 0 48-48 => 0

49 - 48 => 1 49-48 => 1

50 - 48 => 2 50-48 => 2

.. and so-on .. 等等

'0' also represents 48. It is a character form of number 48. That's why, the equation n = 10 * n + (s[i] - '0'); “ 0”也代表48。它是数字48的字符形式。这就是为什么等式n = 10 * n + (s[i] - '0'); has '0'. 具有“ 0”。

In this code what is s[i]-'0' doing? 在这段代码中s[i]-'0'在做什么?

In C, each character like '0' , 'A' , '+' , ' ' is assigned a numeric value or code. 在C语言中,每个像'0''A''+'' ''0' 字符都被分配了一个数值或代码。 C requires that the codes for '0' , '1' , '2' ... '9' are sequential but does not specify their values. C要求'0''1''2' ... '9'是连续的, 但未指定其值。

When code performs the below test, it knows that s[i] has a value within codes '0' and '9' . 当代码执行以下测试时,它知道s[i]的值在代码'0''9' Since these codes are sequential, the only values s[i] could have are '0' , '1' , '2' ... '9' . 由于这些代码是连续的, 唯一的值s[i]可以具有是'0' '1' '2' ... '9'

(s[i]>='0') && (s[i]<='9')

By subtracting '0' from s[i] , code obtains the difference: 通过从s[i]减去'0' ,代码获得了差值:

`0`-'0' --> 0
`1`-'0' --> 1
`2`-'0' --> 2
...
`9`-'0' --> 9

Code has successfully translated the character code for an numeric character into a corresponding integer value. 代码已成功将数字字符的字符代码转换为相应的整数值。

In your function, s is a sequence of ASCII encoded characters: a string. 在您的函数中, s是ASCII编码字符的序列:字符串。 Actually, the encoding does not matter as long as it has the characters 0 through 9 as a sequence. 实际上,编码并不重要,只要它具有字符09作为序列即可。 Let's say it has the value "123ABC" , for this explanation. 对于此说明,假设它的值为"123ABC"

n is the output number. n是输出编号。 At the start of the function it is initialized to zero. 在函数开始时,它被初始化为零。

In the first iteration of the loop we have 在循环的第一个迭代中,我们有

i=0
s[i] = '1'  (which is encoded as 49, in ASCII)
n = 0

so the math is like this: 所以数学是这样的:

n = n * 10 + (s[i] - '0')
n = 0 * 10 + ('1' - '0')
n = '1' - '0'

And converting the character syntax to the ASCII encoding gives this: 并将字符语法转换为ASCII编码可以得到:

n = 49 - 48
n = 1

In the next iteration we have: 在下一次迭代中,我们有:

i = 1
s[i] = '2' (ASCII 50)
n = 1

n = n * 10 + (s[i] - '0')
n = 1 * 10 + ('2' - '0')
n = 10 + ('2' - '0')
n = 10 + (50 - 48)
n = 10 + 2
n = 12

And, in the third iteration: 并且,在第三次迭代中:

i = 2
s[i] = '3' (ASCII 51)
n = 12

n = n * 10 + (s[i] - '0')
n = 12 * 10 + ('3' - '0')
n = 120 + ('3' - '0')
n = 120 + (51 - 48)
n = 120 + 3
n = 123

And, in the final iteration s[i] = 'A' which is not in the range specified by the if statement, so the loop exits. 并且,在最后一次迭代中, s[i] = 'A'不在if语句指定的范围内,因此循环退出。

As you can see it has correctly converted the string "123" to the number 123 . 如您所见,它已将字符串"123"正确转换为数字123

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM