Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can std::string contain binary data

Reply
Thread Tools

Can std::string contain binary data

 
 
Niko Korhonen
Guest
Posts: n/a
 
      01-10-2004
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
 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      01-10-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RCR: String#contain? and Array#contain? Roger Pack Ruby 3 09-28-2010 04:13 PM
Does string contain A, and if so, does a section of string contain B Jason Carlton Javascript 11 12-08-2009 06:07 PM
How can I construct an XML file to contain HTML tags in the data for a Literal element? AAaron123 ASP .Net 5 09-28-2009 05:44 PM
Javascript Error on a GridView templated column where data can contain single quote Anonieko ASP .Net 3 07-06-2006 10:38 AM
Binary stream does not contain a valid BinaryHeader BH ASP .Net 1 08-31-2003 06:53 AM



Advertisments