I have an Arduino that controls timers. The settings for timers are stored in byte arrays. I need to convert the arrays to strings to SET a string on an external Redis server.
So, I have many arrays of bytes of different lengths that I need to convert to strings to pass as arguments to a function expecting char[]
. I need the values to be separated by commas and terminated with '\0'.
byte timer[4] {1,5,23,120};
byte timer2[6] {0,0,0,0,0,0}
I have succeeded to do it manually for each array using sprintf()
like this
char buf[30];
for (int i=0;i<5;i++){ buf[i] = (int) timer[i]; }
sprintf(buf, "%d,%d,%d,%d,%d",timer[0],timer[1],timer[2],timer[3],timer[4]);
That gives me an output string buf
: 1,5,23,120
But I have to use a fixed number of 'placeholders' in sprintf()
.
I would like to come up with a function to which I could pass the name of the array (e.g. timer[]
) and that would build a string, probably using a for loop of 'variable lengths' (depending of the particular array to to 'process') and many strcat()
functions. I have tried a few ways to do this, none of them making sense to the compiler, nor to me!
Which way should I go looking?