CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2008
    Posts
    1

    Lightbulb Loading std::string with binary data

    Hi.
    I'm writing a little communication server and I would like to simplify the building of 'packets'. A packet could send a message, or tell the server to return info on something. I would like to use std::string to store the appropriate data for the packet as my socket wrapper uses it.

    How I would like to build the packet is like so:

    -code-
    int cat=10;
    float chicken=1.26;
    long monkey=123134;

    std::string packet;

    packet << cat << chicken << monkey;
    -code-

    I have looked at std::string stream but it converts anything i throw at it into an ascii representation as so:
    packet == "101.26123134"


    Is there a ready made solution in the STL for this?
    I am trying to avoid using boost.
    Or would just constructing my own class for handling this be easiest?

    Thank you.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Loading std::string with binary data

    For storing binary data you could use std::vector.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Loading std::string with binary data

    Since you're intentionally throwing away type safety anyway, just memcpy to a char array. As a char array can be effortlessly converted to a std::string (and back using c_str()), this won't introduce any need to make changes to the portion that relies on std::string.

    You may need to control the size of the array dynamically, so make sure you know how to do that.

    Also, be careful to make sure that the byte past the end of the part you want in the std::string is 0, and that a 0 byte cannot appear elsewhere in the packet. This would break the conversion to/from std::string. If that is a problem, you may have to forgo the string representation and use the char array directly (along with a byte count).

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Loading std::string with binary data

    1) The code you posted can not compile. Is packet supposed to be
    a stringstream ???

    2) Assuming that packet is a stringstream, you are using operator << ...
    which is formatted output.

    3) Maybe you want unformatted IO

    Code:
    std::stringstream packet;
    
    //  packet << cat << chicken << monkey;
    
    packet.write( reinterpret_cast<char*>(&cat),sizeof(cat) );
    packet.write( reinterpret_cast<char*>(&chicken),sizeof(chicken) );
    packet.write( reinterpret_cast<char*>(&monkey),sizeof(monkey) );
    4) As mentioned above, using packet.str().c_str() might not show
    everything in the stream.

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Loading std::string with binary data

    Agree with Phillip: The OP is proabably thinking of a std::stringstream, not a std::string.

    You can use string::append ( const char* s, size_t n ), to append arbitrary bytes to the end of a std::string, without the type of conversion to an ASCII representation that you are seeing by using the "<<" operator and std::stringstream.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured