Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell a wstring that a string I am feeding it is already a wstring?

Tags:

c++

stl

wstring

I am using a std::string as a text buffer. Then, I am sure the data contained in that buffer is UTF-16 (i.e. it is really a std::wstring). How can I coerce a std::string into a std::wstring? The std::string is a misnomer, the data is really a wstring.

like image 369
unixman83 Avatar asked Jan 24 '26 08:01

unixman83


2 Answers

Consider using a std::vector<char> instead of a std::string. It's the correct container when you want "a contiguous sequence of bytes."

With a std::vector source container, the code is rather straightforward, assuming you really just want to reinterpret the data (i.e., you really just want to treat the bytes as if they were a sequence of wchar_t):

std::vector<char> v = get_my_wstring_character_data();
if (v.size() % sizeof (wchar_t) != 0)
    throw std::runtime_error("Invalid wstring length");

std::wstring ws(reinterpret_cast<wchar_t*>(&v[0]), 
                reinterpret_cast<wchar_t*>(&v[0] + v.size()));

If your source is a std::string, this same approach will work if you can guarantee that the implementation of std::string you are using stores its characters contiguously. In practice, this is always the case.

like image 107
James McNellis Avatar answered Jan 25 '26 21:01

James McNellis


There is no guarantee that std::wstring stores/interprets byte arrays as UTF-16 (although it happens to do that in Windows). Check out this question: std::wstring VS std::string

Therefore I would advise you to rethink the idea of constructing a std::wstring from a UTF-16 encoded byte array unless you are sure your application will only ever be compiled with MSVC.

like image 32
Jon Avatar answered Jan 25 '26 21:01

Jon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!