473,770 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can std::string contain binary data

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<cha r> 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
Jul 22 '05 #1
1 8770
On Sat, 10 Jan 2004 02:45:23 +0200, Niko Korhonen
<ni********@REM OVETHIS.hotmail .com> 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<cha r> 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<cha r> 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
No**********@Ho me.com
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
28199
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
32
49725
by: Wolfgang Draxinger | last post by:
I understand that it is perfectly possible to store UTF-8 strings in a std::string, however doing so can cause some implicaions. E.g. you can't count the amount of characters by length() | size(). Instead one has to iterate through the string, parse all UTF-8 multibytes and count each multibyte as one character. To address this problem the GTKmm bindings for the GTK+ toolkit have implemented a own string class Glib::ustring...
6
5671
by: Erik | last post by:
Hello, For many years ago I implemented my own string buffer class, which works fine except assignments - it copies the char* buffer instead of the pointer. Therefore in function calls I pass "const char*" and get the result in a parameter by reference "MyString& result". I'd like to move to nicer strings. In order to keep platform/compiler independence I consider std:string as the replacement. While googling I managed to read some...
14
12195
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++ books (Effective C++, More Effective C++, Exceptional C++, More Exceptional C++, C++ FAQs, Addison Wesley 2nd Edition) Authors of these books have not considered UNICODE. So many of their
10
9070
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use std::string::size or std::string::empty to deal with this condition? Thank you. string s = ""; if (s == ""); if (s.size == 0); if (s.empty());
6
2348
by: SpreadTooThin | last post by:
How can I read in two characters from a file into a std::string? the long way: fstream f("myfile.bin", ios::binary | ios:in); // Opened for read binary... unsigned char buffer; buffer = '\0'; f.read(buffer, 2); std::string x(buffer);
29
23220
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to write to the file: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) I don't know what I am doing wrong. I have posted my entire program
11
2454
by: Peter Olcott | last post by:
Does C++ have anything like this?
2
5850
by: vermarajeev | last post by:
How to convert a byte array to a std::string. Byte array contains bytes of data. See the code below. Help me to write toStdString() function defined in ByteArray class. int main(int argc, char *argv) { ByteArray byteArray; //is an array of bytes ifstream ifs ( "m1_enc.ct", ios::binary ); //binary file if ( !ifs.is_open () ) { return -1;
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.