C语言 将 ascii 转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4279591/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:12:19  来源:igfitidea点击:

converting a ascii to int

c

提问by ant2009

gcc 4.5.1 c89

海湾合作委员会 4.5.1 c89

I have a buffer that is filled with char characters. I need to compare them:

我有一个充满 char 字符的缓冲区。我需要比较它们:

This is a sample contents of the buffer:

这是缓冲区的示例内容:

vote buffer [ 51 ]
vote buffer [ 32 ]
vote buffer [ 49 ]
vote buffer [ 32 ]
vote buffer [ 50 ]
vote buffer [ 32 ]
vote buffer [ 53 ]
vote buffer [ 32 ]

I am trying to get the int equivalent of these char's that are in the buffer to compare.

我正在尝试获取缓冲区中这些字符的 int 等价物进行比较。

#define NUMBER_OF_CANDIDATES 7
if((vote_data.vote_buff[i] > NUMBER_OF_CANDIDATES || vote_data.vote_buff[i] < 1) {
    /* Do something */
}

As you can see it will never be true in the if statement as the range is far greater.

如您所见,在 if 语句中它永远不会为真,因为范围要大得多。

I have tried casting to (int). However, that didn't solve the problem.

我试过强制转换为 (int)。然而,这并没有解决问题。

I guess I could calculate from the ascii character set. However, I would rather not add more complexity if I cannot help it.

我想我可以从 ascii 字符集计算。但是,如果我不能帮助它,我宁愿不增加更多的复杂性。

Many thanks for any advice,

非常感谢您的任何建议,

采纳答案by Sven Marnach

If you just want to convert single characters to int, you can use c - '0'(which is equivalent to c - 48). If you want to convert strings of more than a single character, use sscanf()

如果您只想将单个字符转换为int,则可以使用c - '0'(相当于c - 48)。如果要转换多个字符的字符串,请使用sscanf()

回答by Juan

You can use atoi to convert strings as integers. Char and int are the same in C

您可以使用 atoi 将字符串转换为整数。C 中的 char 和 int 相同

int main (int argc, char** argv)
{
 int n=65;


 char* String;

 String="1234";


 printf("String: %s - Integer: %d\n", String, atoi(String));


 printf("int %d is char: %c\n ", n, n);

}

回答by wnoise

There is nothing built in to the standard library to turn a charinto an int. This is because most ints don't fit in a char. There are however, several ways to turn a string into an int, because that is much more commonly done. You can easily use these by copying each charinto a the first element of length 2 array with the second char 0, and using this as input to either atoi(), sscanf(), or strtol(). (I'd recommend one of the last two in a real program, as they allow error checking.)

标准库中没有内置任何东西可以将 achar转换为int. 这是因为大多数ints 不适合char. 但是,有几种方法可以将字符串转换为int,因为这种做法更为常见。你可以很容易地通过复制每个使用这些char入长度2阵列的第一元件与所述第二字符0,并且使用该作为输入要么atoi()sscanf()strtol()。(我建议在实际程序中使用最后两个之一,因为它们允许错误检查。)

char buffer[2] = {0,0};
int i;

for (i = 0; i < vote_count; ++i) {
    int vote;
    buffer[0] = vote_data.vote_buff[i];
    vote = atoi(buffer);
    /* handle vote */
}

Using the ASCII values and subtracting '0'is certainly a workable option. Any reasonable character set will have the digits in order.

使用 ASCII 值和减法'0'当然是一个可行的选择。任何合理的字符集都会按顺序排列数字。

Best yet would be to change the interface so that your routine doesn't receive an array of char, but an array of int. The external interfaces of the program should be responsible for sanitizing input and turning it into something easy to process. Currently, there is no way to easily change the program to support more than ten candidates. Letting the input routines store ints fixes this.

最好的方法是更改​​接口,以便您的例程不接收 的数组char,而是接收int. 程序的外部接口应该负责清理输入并将其转换为易于处理的内容。目前,没有办法轻松更改程序以支持十多个候选人。让输入例程存储ints 可以解决这个问题。