In-place substitutions within char arrays?

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?

Loop through the array and test the value returned for each array index. Save the character required back into the array based on what is there already. Leave the last 0 in place as the marker for the end of the string.

If you just want to do it in an editor then search and replace will, or course, do it for you.

But a better question, why do you store those variables, which are clearly hex, in a string?????

You need to adjust the length of your array:

char data[30] = {"0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01"}

which is clearly 39 characters AND you need to add one for the terminating null (\0):

char data[40] = {"0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01"}

And, as @septillion asked - why would you want to do this?

But if you REALLY do want a char string, let the compiler figure out its length:

char data[] = {"0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01"};

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.