Is there a std::streambuf
-like mechanism to convert multi-byte values to network-byte order? In particular, does Boost Asio offer such a primitive? Here is an example of what I would like the stream buffer to do:
uint64_t x = 42ull;
network_streambuf b1;
std::ostream os(&b1);
os << 42ull; // htonll
network_streambuf b2;
std::istream is(&b2);
uint64_t y;
is >> y; // ntohll
EDIT: The answers suggest that this is the wrong way to think about the problem: stream buffers simply provide access to character sequences, their job is not to perform formatted I/O or conversion. I will probably implement a small buffer class that provides the necessary overloads for operator<<
and operator>>
to perform the conversion.
No, and I'll tell you why.
istream::operator>>
and ostream::operator<<
operate on a stream of characters, converting them from their human-readable form to the computer's native form. streambuf
doesn't participate in that conversion at all, except to provide (or accept) a stream of characters.
To put it another way, the formatted I/O routines convert from character form to binary form.
You are asking for a conversion from one binary form to another binary form. That isn't the same thing, and the stream
formatted text routines are the wrong place to look.
That said, you could create your own class that implements operator<<
and operator>>
, and have those routines do network byte swapping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With