如何将ascii字符转换为C中的int

时间:2021-06-29 20:14:03

How can I convert an ascii character into an int in C?

如何将ascii字符转换为C中的int?

8 个解决方案

#1


15  

What about:

关于什么:

int a_as_int = (int)'a';

#2


9  

Are you searching for this:

你在找这个:

int c = some_ascii_character;

Or just converting without assignment:

或者只是转换而不分配:

(int)some_aschii_character;

#3


3  

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'); 

corrected after comments, thanks.

评论后纠正,谢谢。

#4


2  

Use the ASCII to integer function atoi():

使用ASCII到整数函数atoi():

#include <stdlib.h>

int num = atoi("23");

#5


1  

You mean the ASCII ordinal value? Try type casting like this one:

你的意思是ASCII序数值?尝试像这样的类型转换:

int x = 'a';

#6


1  

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://*.com/questions/114611/what-is-the-best-unicode-library-for-c/114643#114643

正如其他人告诉你的那样,你可以直接转换它......除非你的意思是“如何将ASCII扩展字符转换为UTF-16或UTF-32值”。这是一个完全不同的问题(一个至少同样好)。如果我没记错的话,一个相当困难,如果你只使用“纯粹的”C.那么你可以从这里开始:https://*.com/questions/114611/what-is-the-best-unicode-library-对于-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-1 is 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。

#7


1  

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 c in 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 c to 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)打印c的十进制ASCII值,int i = c;将i的ASCII整数值放在i中。您也可以使用(int)c显式转换它。如果您指的是其他内容,例如如何将ASCII数字转换为int,那将是c - '0',它隐式地将c转换为int,然后减去ASCII值'0',即48(在C中) ,由于历史原因,诸如'0'的字符常量是int类型,而不是char。

#8


0  

It is not possible with the C99 standard library, unless you manually write a map from character constants to the corresponding ASCII int value.

除非您手动将字符常量中的映射写入相应的ASCII int值,否则无法使用C99标准库。

Character constants in C like 'a' are not guaranteed to be ASCII.

像'a'这样的C中的字符常量不保证是ASCII。

C99 only makes some guarantees about those constants, e.g. that digits be contiguous.

C99只对这些常数做出了一些保证,例如:这些数字是连续的。

The word ASCII only appears on the C99 N1256 standard draft in footer notes, and footer note 173) says:

单词ASCII仅出现在页脚注释中的C99 N1256标准草案上,页脚注释173)表示:

In an implementation that uses the seven-bit US ASCII character set, the printing characters are those whose values lie from 0x20 (space) through 0x7E (tilde); the control characters are those whose values lie from 0 (NUL) through 0x1F (US), and the character 0x7F (DEL).

在使用7位US ASCII字符集的实现中,打印字符是其值为0x20(空格)到0x7E(代字号)的字符;控制字符是其值从0(NUL)到0x1F(US)的字符,以及字符0x7F(DEL)。

implying that ASCII is not the only possibility

暗示ASCII不是唯一的可能性

#1


15  

What about:

关于什么:

int a_as_int = (int)'a';

#2


9  

Are you searching for this:

你在找这个:

int c = some_ascii_character;

Or just converting without assignment:

或者只是转换而不分配:

(int)some_aschii_character;

#3


3  

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'); 

corrected after comments, thanks.

评论后纠正,谢谢。

#4


2  

Use the ASCII to integer function atoi():

使用ASCII到整数函数atoi():

#include <stdlib.h>

int num = atoi("23");

#5


1  

You mean the ASCII ordinal value? Try type casting like this one:

你的意思是ASCII序数值?尝试像这样的类型转换:

int x = 'a';

#6


1  

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://*.com/questions/114611/what-is-the-best-unicode-library-for-c/114643#114643

正如其他人告诉你的那样,你可以直接转换它......除非你的意思是“如何将ASCII扩展字符转换为UTF-16或UTF-32值”。这是一个完全不同的问题(一个至少同样好)。如果我没记错的话,一个相当困难,如果你只使用“纯粹的”C.那么你可以从这里开始:https://*.com/questions/114611/what-is-the-best-unicode-library-对于-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-1 is 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。

#7


1  

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 c in 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 c to 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)打印c的十进制ASCII值,int i = c;将i的ASCII整数值放在i中。您也可以使用(int)c显式转换它。如果您指的是其他内容,例如如何将ASCII数字转换为int,那将是c - '0',它隐式地将c转换为int,然后减去ASCII值'0',即48(在C中) ,由于历史原因,诸如'0'的字符常量是int类型,而不是char。

#8


0  

It is not possible with the C99 standard library, unless you manually write a map from character constants to the corresponding ASCII int value.

除非您手动将字符常量中的映射写入相应的ASCII int值,否则无法使用C99标准库。

Character constants in C like 'a' are not guaranteed to be ASCII.

像'a'这样的C中的字符常量不保证是ASCII。

C99 only makes some guarantees about those constants, e.g. that digits be contiguous.

C99只对这些常数做出了一些保证,例如:这些数字是连续的。

The word ASCII only appears on the C99 N1256 standard draft in footer notes, and footer note 173) says:

单词ASCII仅出现在页脚注释中的C99 N1256标准草案上,页脚注释173)表示:

In an implementation that uses the seven-bit US ASCII character set, the printing characters are those whose values lie from 0x20 (space) through 0x7E (tilde); the control characters are those whose values lie from 0 (NUL) through 0x1F (US), and the character 0x7F (DEL).

在使用7位US ASCII字符集的实现中,打印字符是其值为0x20(空格)到0x7E(代字号)的字符;控制字符是其值从0(NUL)到0x1F(US)的字符,以及字符0x7F(DEL)。

implying that ASCII is not the only possibility

暗示ASCII不是唯一的可能性