I recently implemented a binary reader/writer for my multiplayer game.
Now reading bytes looks like this:
static int32_t readInt32(std::vector<uint8_t>& msg, int *off)
{
if ((*off) > msg.size()) return 0;
int32_t result; int size = sizeof (result);
memcpy(&result, msg.data() + (*off), size); (*off) += size;
return result;
}
And writing bytes looks like this:
static void writeInt32(std::vector<uint8_t>* msg, int32_t value)
{
uint8_t const * array = reinterpret_cast<uint8_t const *>(&value);
for (std::size_t i = 0; i != sizeof(value); ++i)
{
(*msg).push_back(array[i]);
}
}
How can I improve this?