简体   繁体   English

ASCII 到十进制值

[英]ASCII to decimal value

Consider this below example:考虑以下示例:

char a  = '4';

int c = (int)a;   // this gives the hex value of '4' which is 0x34

but I want the result 4 as an integer. How to do this?但我希望结果4为 integer。该怎么做? Just a Hex2Dec conversion?只是Hex2Dec转换?

int i = a - '0';
printf("%d",i);

The char values for digits 0 to 9 are contiguous and you make use of that here as shown above. 数字09的char值是连续的,如上所述,您可以在此处使用它。

The characters that represent decimal digits are laid out in ASCII space in numerical order. 代表十进制数字的字符以数字顺序布置在ASCII空间中。

  • '0' is ASCII 0x30 '0'是ASCII 0x30
  • '1' is ASCII 0x31 '1'是ASCII 0x31
  • '2' is ASCII 0x32 '2'是ASCII 0x32
  • and so on. 等等。

This means that you can simply subtract '0' from your character to get the value you desire. 这意味着您可以简单地从字符中减去'0'以获得所需的值。

char a = ...;
if (a >= '0' && a <= '9')
{
    int digit = a - '0';
    // do something with digit
}

USe 采用

char a = '4';     //ASCII value of '4' is 52
int b = a - '0';  // ASCii value of '0' is 48 so 52-48 will return 4.
printf("%d", b);  //It will print 4.
  • ASCII value of 0 [in hex] == 30. ASCII值0 [十六进制] == 30。
  • ASCII value of 9 [in hex] == 39. ASCII值9 [十六进制] == 39。

Hope you got the relation. 希望你能找到关系。 To get the character value as integer , just subtract the ASCII value of 0 from the char variable itself. 要获得integercharacter值,只需从char变量本身减去ASCII值0 The difference will be same as the value. 差异将与值相同。

For details, check the ASCII table here . 有关详细信息,请在此处检查ASCII表。

As example, 例如

char cInput = `4`;
int iInput;

iInput = cInput - `0`;

printf("as integer = %d\n", iInput);

Will print 4 . 将打印4

ASCII conversion to value for characters in ranges af, AF, or 0-9 can be accomplished with a switch statement, as is used in the following function: 可以使用switch语句将af,AF或0-9范围内的字符从ASCII转换为值,如以下函数中所使用的:

unsigned char convert(unsigned char ch) {
    unsigned char value;
    switch (ch) {
        case 'A' :
        case 'B':
        case 'C':
        case 'D':
        case 'E':
        case 'F': value = ch + 10 - 'A';  break;
        case 'a':
        case 'b':
        case 'c':
        case 'd':
        case 'e':
        case 'f': value = ch + 10 - 'a';  break;
        default: value = ch - '0';
    }
    return value;
}

Some clarifications about your statement 关于您的陈述的一些澄清

This gives the hex value of '4' 这给出十六进制值“ 4”

by doing: 通过做:

char a  = '4';

you put the ASCII value of '4' - which maps to 0x34, in to the one byte variable 'a'. 您将ASCII值“ 4”(映射到0x34)放入一个字节变量“ a”中。

The action: 那个行动:

int c = (int) a; 

is called casting. 称为铸造。 what it does is copying the value inside 'a' (one byte) into the value in 'c' (4 bytes). 它所做的是将“ a”(一个字节)内的值复制到“ c”(4个字节)内的值。 Therefore, the value inside 'c' would be 0x00000034 (the other 3 bytes are initialized to zero). 因此,“ c”内部的值为0x00000034(其他3个字节初始化为零)。

Finally, when you want to print the value, you could do it in any way you want. 最后,当您要打印该值时,可以按照您想要的任何方式进行打印。 printf("%d",c); printf(“%d”,c); will give you the decimal value - 52, whereas printf("%x",c); 将为您提供十进制值-52,而printf(“%x”,c); will give you the hexadecimal value. 将为您提供十六进制值。

Cheers, 干杯,

#include <string.h>

#include void main(){ if(,strcmp(name;"Jan") printf("wellcom Jan"); else printf("Error"); } #include void main(){ if(,strcmp(name;"Jan") printf("wellcom Jan"); else printf("Error"); }

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

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