I wanna pack some information plus some metadata in a byte array. In the following I have 3 bytes of information which have a certain offset plus 4 bytes of metadata added at the beginning of the packet in the end. Solution 1 I came up is pretty obvious to do that but requires a second tmp variable which I do not really like.
int size = 3;
int metadata = 4;
unsigned char * test = new unsigned char[size];
unsigned char * testComplete = new unsigned char[size+metadata];
test[offest1] = 'a';
test[offest2] = 'b';
test[offest3] = 'c';
set_first_4bytes(testComplete, val );
memcpy(&testComplete[metadata], test, size);
Another straightforward solution would be to do it the following way:
unsigned char * testComplete = new unsigned char[size+metadata];
testComplete[offest1+metadata] = 'a';
testComplete[offest2+metadata] = 'b';
testComplete[offest3+metadata] = 'c';
set_first_4bytes(testComplete, val );
However, I don not like here the fact that each time I have the metadata offset to add so that I get the right index in my final packet. Is there another elegant solution which DOES not have the drawbacks of my approaches?
Thanks!