Function that copies into byte vector reverses values

Posted by xeross on Stack Overflow See other posts from Stack Overflow or by xeross
Published on 2010-04-29T15:18:08Z Indexed on 2010/04/29 15:27 UTC
Read the original article Hit count: 306

Filed under:
|
|

Hey, I've written a function to copy any variable type into a byte vector, however whenever I insert something it gets inserted in reverse.

Here's the code.

template <class Type>
void Packet::copyToByte(Type input, vector<uint8_t>&output)
{
    copy((uint8_t*) &input, ((uint8_t*) &input) + sizeof(Type), back_inserter(output));
}

Now whenever I add for example a uint16_t with the value 0x2f1f it gets inserted as 1f 2f instead of the expected 2f 1f.

What am I doing wrong here ?

Regards, Xeross

© Stack Overflow or respective owner

Related posts about c++

Related posts about vector