Skip to main content
GameDev.net gamedev.net
🔒 Locked

appending data to std::string

Started by RDragon1 Dec 26, 2002 at 10:39 PM 2 replies 250+ views
Original Post
RDragon1
RDragon1
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.
Fruny
Fruny
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Null and Void
Null and Void
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]
antareus
antareus
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.