12

Given

const void * data = ...;
size_t size = ...;

std::string message(???)

How to construct std::string from raw pointer to data and size of the data? data may contain NUL characters.

7
  • If the data could contain zero characters, then it is not a zero-terminated string. In which case it is not clear what you want to get in your std::string. Commented Sep 8, 2015 at 13:10
  • 2
    @DonReba std::string is just a string, not a zero-terminated string Commented Sep 8, 2015 at 13:12
  • 2
    The follow-up question is, do you really need an std::string, or would std::vector<char> be more suitable? Commented Sep 8, 2015 at 13:13
  • @juanchopanza what's the benefit of std::vector<char> over std::string (aside of readability)? Commented Sep 8, 2015 at 13:19
  • @vladon It has a simpler interface which is easier to understand. And it makes the intent clear. It represents a char buffer, not a readable string object. It also doesn't need a \0 tagged on at the end. So it really depends on your use-case. Commented Sep 8, 2015 at 13:20

4 Answers 4

12

string constructor can work with char*, that contains \0, if size is right.

Constructs the string with the first count characters of character string pointed to by s. s can contain null characters. The length of the string is count. The behavior is undefined if s does not point at an array of at least count elements of CharT.

So just use

std::string message(static_cast<const char*>(data), size);
Sign up to request clarification or add additional context in comments.

Comments

7

You can cast data to const char*, then use the std::string two iterator constructor.

const char* sdata = static_cast<const char*>(data);
std::string message(sdata, sdata + size);

Note that is all you need is a byte buffer, it might be simpler and clearer to use an std::vector<unsigned char> instead.

const unsigned char* sdata = static_cast<const unsigned char*>(data);
std::vector<unsigned char> message(sdata, sdata + size);

1 Comment

@vladon No problem. Also note the other answer uses a different constructor. The one I use is common among standard library containers, but std::string has a larger interface.
1

As long as size indicates the correct number, casting is fine. You can have NULL anywhere std::string and you don't even need to NULL terminate your buffer for that matter.

Comments

1

And of course, if you already have a string object

myString.insert(0, static_cast<const char*>(data), size);
                ^^ -- starting index

which calls

basic_string& insert( size_type index, const CharT* s, size_type count );

§

Inserts the first count characters from the character string pointed to by s at the position index. s can contain null characters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.