Jump to content
  • Advertisement
Sign in to follow this  
Arunas

Serializing to a bytestream

This topic is 4003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Is there any way to serialize conventional types like int, double or char to a bytestream? I mean something like this: std::ostringstream ost; ost << 5 << 7.8 << "asdfasd"; BUT std::cout << ost.str().c_str() << '\n' would give "57.8asdfasd" here, and what I need is that 5 and 7.8 would be bytestreamed as they are in memory, but not converted to text format.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Arunas
Hello,

Is there any way to serialize conventional types like int, double or char to a bytestream? I mean something like this:

std::ostringstream ost;

ost << 5 << 7.8 << "asdfasd";

BUT std::cout << ost.str().c_str() << '\n' would give "57.8asdfasd" here, and what I need is that 5 and 7.8 would be bytestreamed as they are in memory, but not converted to text format.
C++ doesn't provide a built-in means of doing this, but you might take a look at the Boost Serialization library (it's probably about as close to a 'standardized' solution to this problem as you're likely to get in C++ - as far as I'm aware, at least).

Share this post


Link to post
Share on other sites
Yes, indeed, I found out that by serializing to archive boost::archive::binary_oarchive I get a bytestream... but not very nice. I get a overhead of 41 bytes always! I mean if I write 5 chars, and two doubles (each 8 bytes) I get my output at size of 62 (which is 5 + 8 + 8 + 41(?)).

If I serialize only std::sring("Hello") I get such bytestream (ascii code and char symbol):
22 ▬
0
0
0
115 s
101 e
114 r
105 i
97 a
108 l
105 i
122 z
97 a
116 t
105 i
111 o
110 n
58 :
58 :
97 a
114 r
99 c
104 h
105 i
118 v
101 e
4 ♦
4 ♦
4 ♦
4 ♦
8
1 ☺
0
0
0
0
0
5 ♣
0
0
0
72 H
101 e
108 l
108 l
111 o

Share this post


Link to post
Share on other sites
Quote:
Original post by Arunas
I get a overhead of 41 bytes always!
I'm guessing that's just header information or something.

Assuming I'm correct about that, what's wrong with having an extra 40 bytes or so tacked onto your archive files? I don't see how that would be a problem...

Share this post


Link to post
Share on other sites
Im using boost::archive::binary_oarchive to form my UDP packets. And you know, if my network code will be sending string "serialization::archive" from one end to another all the time it's kinda funny :-)

Anyway, thanks for guiding me onto Boost Serialization.

Share this post


Link to post
Share on other sites
Quote:
Original post by Arunas
Im using boost::archive::binary_oarchive to form my UDP packets. And you know, if my network code will be sending string "serialization::archive" from one end to another all the time it's kinda funny :-)

Anyway, thanks for guiding me onto Boost Serialization.
Ah, I see :)

Well, I don't know much about networking, so I can't really say whether the Serialization library is the right tool for that particular job (I'm sure others here will be able to give you some good input on the issue though).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!