C语言 如何在C中将ASCII字符转换为int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5322056/
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
How to convert an ASCII character into an int in C
提问by node ninja
How can I convert an ASCII character into an int in C?
如何在 C 中将 ASCII 字符转换为 int?
采纳答案by rjnilsson
What about:
关于什么:
int a_as_int = (int)'a';
回答by UmmaGumma
Are you searching for this:
你在寻找这个:
int c = some_ascii_character;
Or just converting without assignment:
或者只是在没有赋值的情况下进行转换:
(int)some_aschii_character;
回答by Shakespear
Use the ASCIIto Integer atoi()function which accepts a string and converts it into an integer:
使用接受字符串并将其转换为整数的ASCIIto Integeratoi()函数:
#include <stdlib.h>
int num = atoi("23"); // 23
If the string contains a decimal, the number will be truncated:
如果字符串包含小数,则数字将被截断:
int num = atoi("23.21"); // 23
回答by user unknown
I agree to Ashot and Cwan, but maybe you like to convert an ascii-cipher like '7' into an int like 7?
我同意 Ashot 和 Cwan,但也许您喜欢将像 '7' 这样的 ascii-cipher 转换为像 7 这样的 int?
Then I recoomend:
那我建议:
char seven = '7';
int i = seven - '0';
or, maybe you get a warning,
或者,也许你会收到警告,
int i = (int) (seven - '0');
int i = (int) (seven - '0');
corrected after comments, thanks.
评论后更正,谢谢。
回答by Jim Balter
A char value in C is implicitly convertible to an int. e.g, char c; ... printf("%d", c)prints the decimal ASCII value of c, and int i = c;puts the ASCII integer value of cin i. You can also explicitly convert it with (int)c. If you mean something else, such as how to convert an ASCII digit to an int, that would be c - '0', which implicitly converts cto an int and then subtracts the ASCII value of '0', namely 48 (in C, character constants such as '0'are of type int, not char, for historical reasons).
C 中的 char 值可隐式转换为 int。例如,char c; ... printf("%d", c)打印 的十进制 ASCII 值c,int i = c;并将 的 ASCII 整数值c放入i。您也可以使用(int)c. 如果您的意思是其他意思,例如如何将 ASCII 数字转换为 int,那就是c - '0',它隐式转换c为 int,然后减去 的 ASCII 值'0',即 48(在 C 中,诸如'0'int ,而不是字符,由于历史原因)。
回答by Jhourlad Estrella
You mean the ASCII ordinal value? Try type casting like this one:
你的意思是ASCII序数值?尝试像这样的类型转换:
int x = 'a';
回答by xanatos
As everyone else told you, you can convert it directly... UNLESS you meant something like "how can I convert an ASCII Extended character to its UTF-16 or UTF-32 value". This is a TOTALLY different question (one at least as good). And one quite difficult, if I remember correctly, if you are using only "pure" C. Then you could start here: https://stackoverflow.com/questions/114611/what-is-the-best-unicode-library-for-c/114643#114643
正如其他人告诉您的那样,您可以直接转换它......除非您的意思是“我如何将 ASCII 扩展字符转换为其 UTF-16 或 UTF-32 值”。这是一个完全不同的问题(至少同样好)。如果我没记错的话,如果你只使用“纯”C,那么你可以从这里开始:https: //stackoverflow.com/questions/114611/what-is-the-best-unicode-library- for-c/114643#114643
(for ASCII Extended I mean one of the many "extensions" to the ASCII set. The 0-127 characters of the base ASCII set are directly convertible to Unicode, while the 128-255 are not.). For example ISO_8859-1 http://en.wikipedia.org/wiki/ISO_8859-1is an 8 bit extensions to the 7 bit ASCII set, or the (quite famous) codepages 437 and 850.
(对于 ASCII 扩展,我的意思是 ASCII 集的众多“扩展”之一。基本 ASCII 集的 0-127 个字符可直接转换为 Unicode,而 128-255 则不能。)。例如 ISO_8859-1 http://en.wikipedia.org/wiki/ISO_8859-1是 7 位 ASCII 集的 8 位扩展,或者(非常有名的)代码页 437 和 850。

