Reputation: 146
I need a method, how I can get a hexadecimal number from an array, then give it to a variable with the type int. I know that sounds easy but I need it in a "special way" - for example:
I have given a RGB hex color: 01a7ff - and I saved the value of R, G and B in extra arrays - looking like this:
char red[3];
red[0] = '0';
red[1] = '1';
red[2] = '\0';
char green[3];
green[0] = 'a';
green[1] = '7';
green[2] = '\0';
char blue[3];
blue[0] = 'f';
blue[1] = 'f';
blue[2] = '\0';
Now I want to give the complete array of red, green and blue to a malloc reserved array so it looks like this:
char *data;
data = malloc(sizeof(char)*6);
data[0] = 01; //red array
data[1] = a7; //green array
data[2] = ff; //blue array
I first tried using atoi() but that didn't work if the array had also de hexadecimal literals (a,b,c,d,e,f) - has anyone a solution for me?
Cheers
Upvotes: 0
Views: 169
Reputation: 6298
Providing that data holds unsigned char values this should do:
data[0] = (unsigned char) strtol(red, NULL, 16);
data[1] = (unsigned char) strtol(green, NULL, 16);
data[2] = (unsigned char) strtol(blue, NULL, 16);
Note: It is enough to use unsigned char to hold data value from 0 to 255
Upvotes: 1