std::string str(buffer, buffer + length);
Or, if the string already exists:
str.assign(buffer, buffer + length);
Edit: I’m still not completely sure I understand the question. But if it’s something like what JoshG is suggesting, that you want up to length
characters, or until a null terminator, whichever comes first, then you can use this:
std::string str(buffer, std::find(buffer, buffer + length, '\0'));
Related Posts:
- std::string to char*
- Conversion from string to char – c++
- How to convert a char array to a string?
- std::string to char*
- How to convert a std::string to const char* or char*
- How can I convert const char* to string and then back to char*?
- Why the switch statement cannot be applied on strings?
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- Easiest way to convert int to string in C++
- outputting ascii table in C++
- How to convert string to char array in C++?
- C++ — expected primary-expression before ‘ ‘
- C++ string to double conversion
- What is `CString`?
- What is an unsigned char?
- How do I include the string header?
- Differences between C++ string == and compare()?
- How do I convert a double into a string in C++?
- Split a string using C++11
- How do I iterate over the words of a string?
- How to convert an instance of std::string to lower case
- How do I iterate over the words of a string?
- Returning an empty string : efficient way in c++
- C++ string to double conversion
- What is use of c_str function In c++
- How to reverse an std::string? [duplicate]
- C++ Parsing input string to variables
- How to convert a single char into an int [duplicate]
- How do I tokenize a string in C++?
- Why use string::iterator rather than index?
- Why am I getting string does not name a type Error?
- C++ deprecated conversion from string constant to ‘char*’
- getline() does not work if used after some inputs
- identifier “string” undefined?
- Converting a vector
to string - std::string formatting like sprintf
- Sorting Characters Of A C++ String
- basic_string::_M_construct null not valid after constructing subvector of strings
- How to return a string from a C++ function?
- std::out_of_range error?
- What is the difference between isdigit() and isnumber()?
- printf with std::string?
- Convert a String In C++ To Upper Case
- Right way to split an std::string into a vector
- C++: std does not have member “string”
- Right way to split an std::string into a vector
- string subscript out of range error
- C++ String Variable Declaration
- Returning an empty string : efficient way in c++
- Split a string into an array in C++
- splitting a string into an array in C++ without using vector
- Remove spaces from std::string in C++
- Remove spaces from std::string in C++
- c++ convert string to hex
- How can I iterate through a string and also know the index (current position)?
- c++ parse int from string [duplicate]
- Hash function for a string
- C++ strings and pointers
- How to Convert a C++ String to Uppercase
- C++ Simple hangman game
- std::cin input with spaces?
- Remove First and Last Character C++
- C++ Remove punctuation from String
- How to alphabetically sort strings?
- Hash function for a string
- How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?
- Comparing the values of char arrays in C++
- Dynamically allocated string array, then change it’s value?
- Convert char array to single int?
- Fastest way to Convert String to Binary?
- Why I cannot cout a string?
- How to print a string in C++
- std::string length() and size() member functions
- How to convert QString to int?
- Which is faster C++ String length() or size()?
- How do I create a random alpha-numeric string in C++?
- error: switch quantity not an integer
- How to sort with a lambda?
- Reading a string from file c++
- How to change string into QString?
- casting int to char using C++ style casting
- Remove last character from C++ string
- Function stoi not declared
- Converting String to Cstring in C++
- c++ sizeof( string )
- How to do std::string indexof in C++ that returns index of matching string?
- Function stoi not declared
- C++ 2d char array to string
- Understanding error “terminate called after throwing an instance of ‘std::length_error’ what(): basic_string::_S_create Aborted (core dumped)”
- ‘setprecision’ is not a member of ‘std’
- How do I simply compare characters in C++?
- Using cin to input a single letter into a char
- enum to string in modern C++11 / C++14 / C++17 and future C++20
- Converting bool to text in C++
- Why are there two different getline() functions (if indeed there are)?
- Why does my program give “NULL used in arithmetic”
- How to find and replace string?
- Declaration is incompatible with type
- I’m getting the error “stoi is not a member of std” in myprogramminglab [duplicate]