Can std::string contain binary data

N

Niko Korhonen

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
 
B

Bob Hairgrove

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top