从数组中获取 RGB 十六进制值并将其作为 int 传递给 malloc 数组

Get the RGB-hexadecimal values from an array and give it to an malloc-array as int

我需要一个方法,如何从数组中获取一个十六进制数,然后将它赋给一个 int 类型的变量。我知道这听起来很简单,但我需要它在 "special way" 中 - 例如:

我给出了 RGB 十六进制颜色:01a7ff - 我将 R、G 和 B 的值保存在额外的数组中 - 如下所示:

char red[3];
red[0] = '0';
red[1] = '1';
red[2] = '[=10=]';

char green[3];
green[0] = 'a';
green[1] = '7';
green[2] = '[=10=]';

char blue[3];
blue[0] = 'f';
blue[1] = 'f';
blue[2] = '[=10=]';

现在我想将完整的红色、绿色和蓝色数组提供给 malloc 保留数组,如下所示:

char *data;
data = malloc(sizeof(char)*6);

data[0] = 01; //red array
data[1] = a7; //green array
data[2] = ff; //blue array

我首先尝试使用 atoi() 但如果数组也有十六进制文字 (a,b,c,d,e,f),那将不起作用 - 有没有人为我提供解决方案?

干杯

如果数据包含 unsigned char 值,应该这样做:

data[0] = (unsigned char) strtol(red, NULL, 16);
data[1] = (unsigned char) strtol(green, NULL, 16);
data[2] = (unsigned char) strtol(blue, NULL, 16);

注意:使用unsigned char可以保存0到255的数据值