Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you construct a std::string with an embedded null?

If I want to construct a std::string with a line like:

std::string my_string("a\0b"); 

Where i want to have three characters in the resulting string (a, null, b), I only get one. What is the proper syntax?

like image 465
Bill Avatar asked Oct 02 '08 19:10

Bill


People also ask

Can std::string have embedded nulls?

Yes you can have embedded nulls in your std::string .

How do you add a null character to a string in C++?

c_str() method, you get an array of characters with the '\0' character at the end. @alegen none of that changes anything. Usually \0 indicates the end of a string. So if you pass c_str() with nulls in the middle to a function expecting a null terminated string it won't use the full string, just up to the first null.

How do you write a null character in a string?

'\0' is defined to be a null character. It is a character with all bits set to zero.

Can a string be made up of only a null character?

A string can be made up of only a null character.


2 Answers

Since C++14

we have been able to create literal std::string

#include <iostream> #include <string>  int main() {     using namespace std::string_literals;      std::string s = "pl-\0-op"s;    // <- Notice the "s" at the end                                     // This is a std::string literal not                                     // a C-String literal.     std::cout << s << "\n"; } 

Before C++14

The problem is the std::string constructor that takes a const char* assumes the input is a C-string. C-strings are \0 terminated and thus parsing stops when it reaches the \0 character.

To compensate for this, you need to use the constructor that builds the string from a char array (not a C-String). This takes two parameters - a pointer to the array and a length:

std::string   x("pq\0rs");   // Two characters because input assumed to be C-String std::string   x("pq\0rs",5); // 5 Characters as the input is now a char array with 5 characters. 

Note: C++ std::string is NOT \0-terminated (as suggested in other posts). However, you can extract a pointer to an internal buffer that contains a C-String with the method c_str().

Also check out Doug T's answer below about using a vector<char>.

Also check out RiaD for a C++14 solution.

like image 93
Martin York Avatar answered Sep 22 '22 16:09

Martin York


If you are doing manipulation like you would with a c-style string (array of chars) consider using

std::vector<char> 

You have more freedom to treat it like an array in the same manner you would treat a c-string. You can use copy() to copy into a string:

std::vector<char> vec(100) strncpy(&vec[0], "blah blah blah", 100); std::string vecAsStr( vec.begin(), vec.end()); 

and you can use it in many of the same places you can use c-strings

printf("%s" &vec[0]) vec[10] = '\0'; vec[11] = 'b'; 

Naturally, however, you suffer from the same problems as c-strings. You may forget your null terminal or write past the allocated space.

like image 45
Doug T. Avatar answered Sep 25 '22 16:09

Doug T.