Can std::string contain binary data

Discussion in 'C++' started by Niko Korhonen, Jan 10, 2004.

  1. I'm currently in the process of programming a multimedia tagging library
    in standard C++. However, I've stumbled across one or two unclear issues
    while working with the library.

    First of all, is it safe to store binary data in std::string? This
    question rose from my implementation with APEv2 tags. An APEv2 tag's field
    value can contain either UTF encoded text or binary data. I've decided to
    use std::string to represent the field value. This value will be plain
    text in 99% of the cases, but there still is an offside chance that
    someone will shove binary data into these tags.

    Is there anything I should know about strings with binary data, do they
    perform some automatic formatting or something like that? Something that
    can possibly be dangerous to the data?

    I considered using std::vector<char> to represent the field value, but it
    was extremely inconvinient. I had to convert the vector to a string or
    char* all over the place, because after all, strings and char*'s are the
    most common use case.

    Greets,
    Niko Korhonen
     
    Niko Korhonen, Jan 10, 2004
    #1
    1. Advertising

  2. On Sat, 10 Jan 2004 02:45:23 +0200, Niko Korhonen
    <> wrote:

    >I'm currently in the process of programming a multimedia tagging library
    >in standard C++. However, I've stumbled across one or two unclear issues
    >while working with the library.
    >
    >First of all, is it safe to store binary data in std::string? This
    >question rose from my implementation with APEv2 tags. An APEv2 tag's field
    >value can contain either UTF encoded text or binary data. I've decided to
    >use std::string to represent the field value. This value will be plain
    >text in 99% of the cases, but there still is an offside chance that
    >someone will shove binary data into these tags.
    >
    >Is there anything I should know about strings with binary data, do they
    >perform some automatic formatting or something like that? Something that
    >can possibly be dangerous to the data?
    >
    >I considered using std::vector<char> to represent the field value, but it
    >was extremely inconvinient. I had to convert the vector to a string or
    >char* all over the place, because after all, strings and char*'s are the
    >most common use case.
    >
    >Greets,
    >Niko Korhonen


    AFAIK it is possible to store binary data in a std::string. You will
    most likely encounter no problems as long as you don't use functions
    like std::string::c_str(), which wouldn't make sense if embedded null
    bytes were in the string.

    However, I would prefer a vector<char> or vector<unsigned char>)
    myself because that is actually what you are dealing with. Clients ho
    see std::string normally expect the data to be character data, not
    binary data. Besides, the most recent C++ standard specifies that
    storage for vectors must be contiguous in memory, and most (all?) of
    the popular compilers implement it this way, so you can always use
    something like this:

    #include <vector>
    int main() {
    std::vector<char> MyBytes(100); // reserve space for 100 bytes
    // fill up vector here...
    char *elem = &(MyBytes[0]);
    // use elem as array of char ...
    return 0;
    }


    --
    Bob Hairgrove
     
    Bob Hairgrove, Jan 10, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,348
    Ivan Vecerina
    Mar 17, 2005
  2. Jeffrey Walton
    Replies:
    10
    Views:
    955
    Mathias Gaunard
    Nov 26, 2006
  3. Leslaw Bieniasz
    Replies:
    9
    Views:
    6,031
    James Kanze
    Feb 26, 2008
  4. Roger Pack
    Replies:
    3
    Views:
    166
    Roger Pack
    Sep 28, 2010
  5. Jason Carlton
    Replies:
    11
    Views:
    251
    Dr J R Stockton
    Dec 8, 2009
Loading...

Share This Page