I am trying to store two integer value into an char array in C++.
Here is the code..
char data[20];
*data = static_cast <char> (time_delay); //time_delay is of int type
*(data + sizeof(int)) = static_cast<char> (wakeup_code); //wakeup_code is of int type
Now on the other end of the program, I want to reverse this operation. That is, from this char array, I need to obtain the values of time_delay and wakeup_code.
How can I do that??
Thanks,
Nick
P.S: I know this is a stupid way to do this, but trust me its a constraint.
I think when you write
static_cast<char>, that value is converted to a 1-byte char, so if it didn’t fit in a char to begin with, you’ll lose data.What I’d do is use
*((int*)(data+sizeof(int)))and*((int*)(data+sizeof(int)))for both reading and writing ints to the array.Alternatively, you might also write: