简体   繁体   English

如何从C中的char获取int

[英]How to get int from char in C

(i'm french, sorry for my bad english) (我是法国人,对不起我的英语不好)

I don't know how to get an int from a char[], the patern of the char will be the same everytime : "prendre 2", "prendre 44", "prendre 710"... 我不知道如何从char []获得一个int,每次的char模式都相同:“ prendre 2”,“ prendre 44”,“ prendre 710” ...

I want to check if the pattern of the sentence is correct and get the integer. 我想检查句子的模式是否正确并获取整数。

I have try to do this, but as you see, the problem is i just can check if the integer is between 0-9 because I check only one char. 我已经尝试执行此操作,但是如您所见,问题是我只能检查整数是否在0-9之间,因为我只检查了一个字符。

[...]

else if (est_prendre(commande)){
    /* if  the output is 1*/
    int number = commande[8]- '0'
}
int est_prendre(char *commande){
    int i;
    char temp[9] = "";
    char c = commande[8];
    int num = c - '0';
    for (i=0; i<8; i++){
        temp[i] = commande[i];
    }
    if (strcmp ("prendre ", temp) == 0)
    {
        if ( /*  num IS INTEGER?  */)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    } else {
        return 0;
    }

}

I expect if commande = "prendre 3", output of est_prendre is 1 because the pattern is correct And after than to put the integer into the variable number. 我期望如果commande =“ prendre 3”,则est_prendre的输出为1,因为模式正确,然后将整数放入变量号中。

Thank You! 谢谢!

This is very basic, you should (re)read any reference/tutorial on C that you have used to learn the language. 这是非常基础的,您应该(重新)阅读用于学习该语言的任何有关C的参考/教程。

You should just use the sscanf() standard function: 您应该只使用sscanf()标准函数:

int value;
if (sscanf(commande, "prendre %d", &value) == 1)
{
  ... it was a match, the variable 'value' will be set to the number from the string
}

you can delete the (strange-looking) code that copies characters from commande to temp , and the temp variable too of course. 您可以删除将字符从commande复制到temp的(看起来很奇怪)代码,当然也删除temp变量。 Just inspect the commande string directly. 只需直接检查commande字符串即可。

Assuming that 'commande + 8' is a valid substring, what you need is atoi() function (ASCII to Integer). 假设“ commande + 8”是有效的子字符串,那么您需要的是atoi()函数(ASCII到Integer)。 This function is widely documented, and you can easily find online its usage. 此功能已被广泛记录,您可以在网上轻松找到其用法。

int number = atoi(commande+8);

Just remember that the substring terminates at the first non-digit character: 请记住,子字符串在第一个非数字字符处终止:

  • atoi("23") returns 23 atoi(“ 23”)返回23
  • atoi("51hh37") returns 51 atoi(“ 51hh37”)返回51
  • atoi("z3456") returns 0 atoi(“ z3456”)返回0

Note: atoi converts input string into integer, and you can use it if you are sure it fits expected input. 注意:atoi将输入字符串转换为整数,如果确定它适合预期的输入,则可以使用它。 So, if you expect to have either long integers or float values in your string you can use atol() (ASCII to long) or atof() (ASCII to float). 因此,如果您希望字符串中包含长整数或浮点值,则可以使用atol()(ASCII转换为long)或atof()(ASCII转换为浮点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM