capanga09:
Hi everyone!
quick question: how can I substitute some values in a char array? For instance, if I have:
char data[30] = {"0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01"}
and would like to turn the 0x00 into "1", and 0x01 into "0" and have it all stored as a char array:
char data2[30] = {"1,1,1,1,1,1,1,0"};
any suggestions on how I could go about doing this?
To restate your problem in programmer talk: "I have a string with comma-separated hex constants, and I want I want to parse them into an array of byte".
The keyword is "parse". since your string has a nice, regular format,
copy the whole string to a buffer, using strncpy (unless you are happy to destroy the string).
break out the data in that buffer using strtok
use atoi to convert the stings into values.
// Warning! This will destroy the contents of src!
int parseHexValues(char *src, byte *dest) {
int n = 0;
for(char *p = strtok(src, ", \t"); p; p = strtok(NULL, ", \t")) {
dest[n++] = atoi(p+2, 16);
}
return n;
}
I suppose you can do this in-place if you want by passing in the same src and dest.