I'm trying to set up writing uint32_ts into a buffer that I'm going to send across the wire but I'm not sure how to do it correctly. Initially I assumed that operator<< would handle it correctly and write the 4 bytes to the buffer (assuming I had used htonl before). Instead I started getting values that didn't make any sense. I noticed it when sometimes writing a single int to the stream sometimes increased the size of buffer by up to ten bytes instead of the expected four. I wrote up a short test and realized that it's actually dumping the integers in ascii to the buffer. I think I've figured out a way around it but it feels hacky, so I'm looking for a better way, or justification on why what I'm doing is actually right.
Here'ssome example code:
boost::asio::streambuf buffer;
std::ostream os(&buffer);
uint32_t value = 0x12345678;
os << value; // Gives ascii 305419896
os.write((const char *) &value, sizeof(value)); // fives 0x12345678
Is there a better way to do this than my last line of code?
Aucun commentaire:
Enregistrer un commentaire