简体   繁体   中英

C - Convert char to int

I know that to convert any given char to int, this code is possible [apart from 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.

Thanks in advance!!

In C, a character literal has type int . [ Character Literals/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 . This works for ASCII numbers from 0 to 9.

See ASCII table to get a better picture.

Edit: Thanks to @ouah for correction.

All the chars in C are represented with an integer value, the ASCII code of the character. For instance '0' corresponds to 48 and '2' corresponds to 50, so '2'-'0' gets you 50-48 = 2

Link to an ASCII table: 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. In fact, such literals evaluate not to char , but to int , so they are perfectly interchangeable with other number literals.

Within your expression, '2' is interchangeable with 50 , and '0' is interchangeable with 48 .

Have a look at the ASCII table.

'0' is represented as 0x30, '9' is represented as 0x32.

This results in

0x32 - 0x30 = 2

It's all about the ASCII codes of the corresponding characters.

In C, all the digits (0 to 9) are encoded in ASCII by values 48 to 57, sequentially. So '0' actually gets value 48, and '2' has the value 50. So when you write int i = '2' - '0'; , you're actually subtracting 48 from 50, and get 2.

'0' to '9' are guaranteed to be sequential values in C in all character sets. This not limited to ASCII and C is not limited to the ASCII character set.

So sequential here means that '2' value is '0' + 2 .

Regarding int and char note that '0' and '9' values are of type int in C and not of type char . A character literal is of type 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.

You can do similar tricks with characters as well, eg shift lowercase to uppercase by subtracting 32 from a lowercase character

'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'.

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.

This means that '2' - '0' is translated to 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

You can look up the numeric representaions of each ASCII character in thsi table: http://www.asciitable.com/

Actually a char is just a unsigned byte: C just treats it differently for different operations. For example printf(97) yields 97 as output, but printf((char)97) will give you 'a' as output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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