Converting a C-string to a std::vector<byte> in an efficient way

c++, vector

Solution

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;

Problem

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?

Original source