I wanted to write a quick test program to send a message with variable length fields in the message. Typically I would use some sort of dynamically allocated c-like buffer and just memcpy the elements of different sized into their proper positions. For example:
I would like to use the STL to do this a little more elegantly and c++-ish, but I don't know how to go about doing it the best way. I could do something like this:
However, that seems worse to me. Could someone give me some ideas on a good way to handle packing data words of different sizes into an STL container?
Thanks!
Btw... I didn't actually attempt to compile this so it may not be 100% syntactically correct.
Code:
char Buffer[8192]; unsigned short count = 512; char temp_char = 3, temp_char2 = 255; *(unsigned short*)(&Buffer[0]) = 1; *(unsigned short*)(&Buffer[2]) = count; Buffer[4] = temp_char; Buffer[5] = temp_char2;
Code:
deque<char> Buffer(8192); deque<char>::iterator buff_iter = Buffer.begin(); unsigned short count = 512; char temp_char = 3, temp_char2 = 255; *buff_iter = 0; // High Byte buff_iter++; *buff_iter = 1; // Low Byte buff_iter++; *buff_iter = ((count >> 8) & 0xFF); // High Byte buff_iter++; *buff_iter = (count & 0xFF); // Low Byte buff_iter++; *buff_iter = temp_char; buff_iter++; *buff_iter = temp_char2;
Thanks!
Btw... I didn't actually attempt to compile this so it may not be 100% syntactically correct.
Comment