I have a std::vector of bytes (char
), I'd like to do the equivalent of just "C-style casting" this vector to a vector which is of type wchar_t
.
Obviously, what I really have to do is to copy the data, but the thing here is that I already have an UTF-16 byte stream on the left side, I just wanna move that over to the wchar_t
vector so that I can use it. Ideally, I'd like to just swap the buffer, but I'm not sure how to do that in safe manner...
What's the C++ way of doing an as efficient as safe conversion copy operation allows?
I do store my UTF-16 strings as std::wstring
or std::vector<wchar_t>
but I have this memory buffer that I happen to know is UTF-16, and I need to copy it, somehow...
The most efficient (and sanest) way to do it is to not do it. Let your vector<char>
own the data buffer, and simply create a pair of wchar_t
pointers to use as iterators pointing into the vector.
std::vector<char> vec;
wchar_t* first = reinterpret_cast<wchar_t*>(&vec[0]);
wchar_t* last = reinterpret_cast<wchar_t*>(&vec[0] + vec.size());
Now you have an iterator pair that'll work just fine with all the standard library algorithms. And you didn't have to copy a single byte. :)
(Disclaimer: I'm assuming that the vector's size is divisible by sizeof(wchar_t)
. Otherwise you'll have to adjust the last
pointer)
std::vector<char> v1;
std::vector<wchar_t> v2;
const char * cv1 = v1.data();
const wchar_t * cv2 = static_cast<const wchar_t *>(cv1);
std::copy(cv2, cv2 + v1.size() / sizeof(wchar_t), std::back_inserter(v2));
std::vector<char> v1;
std::vector<wchar_t> v2;
wchar_t *begin = (wchar_t *) &v2.front();
wchar_t *end = (wchar_t *) (&v2.back() + 1);
v1.assign(begin, end);
I haven't tested this, but I can't imagine that something like this wouldn't work... If you have endian issues, this would become quite a bit more complicated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With