将char字符串解析为INT C编程

use*_*460 1 c string int char

我正在尝试将char字符串解析为INT.

如果我有...

unsigned char color[] = "255"
Run Code Online (Sandbox Code Playgroud)

并希望将其解析为INT.我该怎么做呢?

我试过了...

unsigned char *split;

split = strtok(color," ,.-");
while(split != NULL)
{
    split = strok(NULL, " ,.-);
}
Run Code Online (Sandbox Code Playgroud)

这只是给了我现在拆分的值255.

我觉得我需要像......

int y = split - '0';   //but this makes an INT pointer without a cast
Run Code Online (Sandbox Code Playgroud)

flu*_*ter 5

要将字符串转换为整数,请调用strtol:

char color[] = "255";
long n;
char *end = NULL;
n = strtol(color, &end, 10);
if (*end == '\0') {
    // convert was successful
    // n is the result
}
Run Code Online (Sandbox Code Playgroud)