Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

RDragon1

appending data to std::string

This topic is 5752 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

hey all, i''m trying to code a class for outgoing packets in a network app, the way i''m trying to get it to work is to have the class''s internal buffer a std::string, and as i assemble the packet with data i can do things such as mypacket += (long)0xFF; mypacket += strLoginName; mypacket += (short)0x0102; etc etc... i understand how to overload the += operator, but i''m not entirely sure how i can append things such as long''s, and short''s to the string as exactly the data they are (ie if i have (long)32 i dont want to append "32" i want to append "\32\0\0\0" as in, 4 bytes, of exactly what data they are someone suggested i use stringstreams for this, but that converts the values into text and is not what i want, im trying to make a data buffer for packets to throw through a socket.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by RDragon1
someone suggested i use stringstreams for this, but that converts the values into text and is not what i want, im trying to make a data buffer for packets to throw through a socket.

You should use stringstream with the write member function. To get the data back out, use stringstream's str member function (returns a string), and string's data member function to get the data from that string. Edit: not fast enough .



[edited by - Null and Void on December 26, 2002 11:48:07 PM]

Share this post


Link to post
Share on other sites
std::string supposedly has a constructor that takes a C style string and its length. I have not tried it - I looked it up in Stroustrup this week.

Share this post


Link to post
Share on other sites

  • 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!