C语言 ASCII 到十进制值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27501243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASCII to decimal value
提问by PlatonInc.
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 4as an integer. How to do this? Just a Hex2Decconversion?
但我希望结果4为整数。这该怎么做?只是Hex2Dec转换?
回答by Gopi
int i = a - '0';
printf("%d",i);
The char values for digits 0to 9are contiguous and you make use of that here as shown above.
对于数字的字符值0来9是连续的,你利用这里上面,如图所示。
回答by David Heffernan
The characters that represent decimal digits are laid out in ASCII space in numerical order.
代表十进制数字的字符按数字顺序排列在 ASCII 空间中。
'0'is ASCII 0x30'1'is ASCII 0x31'2'is ASCII 0x32- and so on.
'0'是 ASCII 0x30'1'是 ASCII 0x31'2'是 ASCII 0x32- 等等。
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
}
回答by Tom Ritch
ASCII conversion to value for characters in ranges a-f, A-F, or 0-9 can be accomplished with a switch statement, as is used in the following function:
可以使用 switch 语句将 ASCII 转换为 af、AF 或 0-9 范围内的字符值,如以下函数中所使用:
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;
}
回答by Himanshu
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.
回答by Sourav Ghosh
- ASCII value of
0[in hex] == 30. - ASCII value of
9[in hex] == 39.
0[十六进制]的ASCII 值== 30。9[十六进制]的ASCII 值== 39。
Hope you got the relation. To get the charactervalue as integer, just subtract the ASCII value of 0from the charvariable itself. The difference will be same as the value.
希望你得到了关系。要获得characteras的值integer,只需0从char变量本身减去 的 ASCII 值。差异将与值相同。
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。
回答by nerez
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'.
您将 '4' 的 ASCII 值(映射到 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). Therefore, the value inside 'c' would be 0x00000034 (the other 3 bytes are initialized to zero).
称为铸造。它所做的是将“a”(一个字节)中的值复制到“c”(4 个字节)中的值中。因此,'c' 中的值将是 0x00000034(其他 3 个字节被初始化为零)。
Finally, when you want to print the value, you could do it in any way you want. printf("%d",c); will give you the decimal value - 52, whereas printf("%x",c); will give you the hexadecimal value.
最后,当您想打印该值时,您可以以任何您想要的方式进行。printf("%d",c); 会给你十进制值 - 52,而 printf("%x",c); 会给你十六进制值。
Cheers,
干杯,

