Archived

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

RDragon1

appending data to std::string

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
As with the other streams, stringstream supports binary IO using the read() and write() member functions.





[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
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