[英]How to convert a char to its ascii decimal value?
I need to take a keyboard input letter, and save that letter's decimal value as an integer. 我需要一个键盘输入字母,并将该字母的十进制值保存为整数。 How can I do that with scanf ?
我如何用scanf做到这一点?
I just figured out myself. 我只是想通了自己。
if I want to store the decimal value of 'z' 如果我想存储十进制值“ z”
I can just do int value='z'-'a'+ 97 我可以做int value ='z'-'a'+ 97
This is what I wanted to know. 这就是我想知道的。
This code reads a char from keyboard ( stdin
) using scanf()
, stores it in byte-sized variable c
of type char
, and then prints its ASCII decimal value as an int
to stdout
: 这段代码使用
scanf()
从键盘( stdin
)读取一个char,将其存储在char
类型的字节大小的变量c
中,然后将其ASCII十进制值作为int
打印到stdout
:
char c;
scanf("%c", &c);
printf("%d", c);
If you want to output the ASCII value, simply printf()
with the %d
format string. 如果要输出ASCII值,只需使用带有
%d
格式字符串的printf()
即可。
char ch = 'a';
printf("%d", a); // should be 97
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.