Reputation: 17554
I want to convert a C-style string into a byte-vector. A working solution would be converting each character manually and pushing it on the vector. However, I'm not satisfied with this solution and want to find a more elegant way.
One of my attempts was the following:
std::vector<byte> myVector;
&myVector[0] = (byte)"MyString";
which bugs and gets me an
error C2106: '=': left operand must be l-value
What is the correct way to do this?
Upvotes: 5
Views: 25048
Reputation: 365
In case you would not like to copy the original string and would just like to iterate over it as an array of bytes, then C++20 has std::span
to offer.
auto const const_bytes = std::as_bytes(std::span{str.data(), str.size()});
std::span<const std::byte>
provides std::vector
like iterating capabilities which I think is what you might be looking for.
Note: The original string would have to remain valid for the entire scope of std::span
variable
Upvotes: 2
Reputation: 473
const char *cstr = "bla"
std::vector<char> vec;
vec.resize(strlen(cstr)+1);
strcpy(&vec[0],cstr);
Upvotes: -1
Reputation: 503855
The most basic thing would be something like:
const char *cstr = "bla"
std::vector<char> vec(cstr, cstr + strlen(cstr));
Of course, don't calculate the length if you know it.
The more common solution is to use the std::string
class:
const char *cstr;
std::string str = cstr;
Upvotes: 9
Reputation: 13526
Something along these lines should work
std::vector<byte> myVector = std::vector((byte*)cstring, (byte*)ctring + strlen(cstring))
Also, this is still going to just iterate through the c string and insert the values into the vector. Like Konrad said, that's just how you have to do it since vectors manage their own memory.
Upvotes: 1
Reputation: 545588
STL containers such as vector
always take ownership, i.e. they manage their own memory. You cannot modify the memory managed internally by an STL container. For that reason, your attempt (and any similar attempt) is doomed to failure.
The only valid solution is to copy the memory or to write a new STL-style container that doesn’t take ownership of the memory it accesses.
Upvotes: 5
Reputation: 490128
The most obvious question would be why you don't just use std::string:
std::string myString("MyString");
but if you really think you need a vector:
char myString[] = "MyString";
std::vector<byte> myVector(myString, myString+sizeof(myString));
You might also want to consider using std::tr1::array:
std::tr1::array<byte, sizeof("MyString")> myArray = {"MyString"};
C++ 0x will also have std::array
.
Upvotes: 0
Reputation: 99585
std::vector<byte> myVector;
const char* str = "MyString";
myVector.assign( str, str+strlen(str) );
Upvotes: 0