简体   繁体   English

C-将char转换为int

[英]C - Convert char to int

I know that to convert any given char to int, this code is possible [apart from atoi()]: 我知道要将任何给定的char转换为int,此代码是可能的[除了atoi()之外:

int i = '2' - '0';

but I never understood how it worked, what is significance of '0' and I don't seem to find any explanation on the net about that. 但我从来不了解它是如何工作的,“ 0”的含义是什么,我似乎也没有在网上找到任何有关它的解释。

Thanks in advance!! 提前致谢!!

In C, a character literal has type int . 在C中,字符文字的类型为int [ Character Literals/IBM ] [ 字符文字/ IBM ]

In your example, the numeric value of '0' is 48, the numeric value of '2' is 50. When you do '2' - '0' you get 50 - 48 = 2 . 在您的示例中,数值'0'为48,数值'2'为50。当您执行'2' - '0' ,得到50 - 48 = 2 This works for ASCII numbers from 0 to 9. 适用于从0到9的ASCII数字。

See ASCII table to get a better picture. 请参阅ASCII表以获得更好的图像。

Edit: Thanks to @ouah for correction. 编辑:感谢@ouah的更正。

All the chars in C are represented with an integer value, the ASCII code of the character. C中的所有字符都用整数值表示,即字符的ASCII码。 For instance '0' corresponds to 48 and '2' corresponds to 50, so '2'-'0' gets you 50-48 = 2 例如,“ 0”对应于48,而“ 2”对应于50,因此'2'-'0'使您50-48 = 2

Link to an ASCII table: http://www.robelle.com/smugbook/ascii.html 链接到ASCII表: http : //www.robelle.com/smugbook/ascii.html

当使用逗号' '您将数字视为一个字符,并且如果将其提供给int,则int将采用此字符的ASCII代码的值。

Any character literal enclosed in single quotes corresponds to a number that represents the ASCII code of that character. 用单引号引起来的任何字符文字都对应于代表该字符的ASCII码的数字。 In fact, such literals evaluate not to char , but to int , so they are perfectly interchangeable with other number literals. 实际上,此类文字不算作char ,而是int ,因此它们可以与其他数字文字完全互换。

Within your expression, '2' is interchangeable with 50 , and '0' is interchangeable with 48 . 在表达式中, '2'可与50互换,而'0'可与48互换。

Have a look at the ASCII table. 看一下ASCII表。

'0' is represented as 0x30, '9' is represented as 0x32. '0'表示为0x30, '9'表示为0x32。

This results in 这导致

0x32 - 0x30 = 2

It's all about the ASCII codes of the corresponding characters. 都是关于相应字符的ASCII码

In C, all the digits (0 to 9) are encoded in ASCII by values 48 to 57, sequentially. 在C语言中,所有数字(0至9)都按ASCII值48至57顺序编码。 So '0' actually gets value 48, and '2' has the value 50. So when you write int i = '2' - '0'; 因此, '0'实际上得到值48,而'2'具有值50。因此,当您写int i = '2' - '0'; , you're actually subtracting 48 from 50, and get 2. ,实际上是从50中减去48,得到2。

'0' to '9' are guaranteed to be sequential values in C in all character sets. 在所有字符集中,确保C中的'0''9'是顺序值。 This not limited to ASCII and C is not limited to the ASCII character set. 这不限于ASCII,C也不限于ASCII字符集。

So sequential here means that '2' value is '0' + 2 . 因此,这里的顺序表示'2'值为'0' + 2

Regarding int and char note that '0' and '9' values are of type int in C and not of type char . 关于intchar请注意, '0''9'值在C语言中为int类型,而不是char类型。 A character literal is of type int . 字符文字的类型为int

Both terms are internally represented by the ASCII code of the number, and as numeric digits have consecutive ASCII codes subtracting them gives you the difference between the two numbers. 这两个术语在内部均由数字的ASCII码表示,并且数字具有连续的ASCII码,将它们相减可得出两个数字之间的差。

You can do similar tricks with characters as well, eg shift lowercase to uppercase by subtracting 32 from a lowercase character 您也可以对字符进行类似的操作,例如,通过从小写字符中减去32将小写字母转换为大写字母

'a' - 32 = 'A'

This works only because ASCII assigns codes to characters in order ie '2' has a character code that is with 2 bigger than the character code of '0'. 这仅是因为ASCII按顺序将代码分配给字符,即“ 2”的字符代码比“ 0”的字符代码大2。

In an another encoding it wouldn't work. 在另一种编码中,它将不起作用。

When you cast a char to an int it actually maps each character to the appropriate number in the ascii table. 当将char转换为int时,它实际上将每个字符映射到ascii表中的适当数字。

This means that '2' - '0' is translated to 50 - 48 . 这意味着'2' - '0'被转换为50 - 48 So you could also find out the numeric distance of two letters in the same way, eg 'z' - 'a' equals 122 - 97 equals 25 因此,您也可以用相同的方式找出两个字母的数字距离,例如'z' - 'a'等于122 - 97等于25

You can look up the numeric representaions of each ASCII character in thsi table: http://www.asciitable.com/ 您可以在此表中查找每个ASCII字符的数字表示形式: http : //www.asciitable.com/

Actually a char is just a unsigned byte: C just treats it differently for different operations. 实际上, char只是一个无符号字节:C只是针对不同的操作将其区别对待。 For example printf(97) yields 97 as output, but printf((char)97) will give you 'a' as output. 例如, printf(97)产生97作为输出,但是printf((char)97)将给您'a'作为输出。

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

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