Take the 2-minute tour ×

I am trying to convert a std::string Buffer - containing data from a bitmap file - to std::wstring.

I am using MultiByteToWideChar, but that does not work, because the function stops after it encounters the first '\0'-character. Seems like it interprets it as the end of the string.

When i dont pass -1 as the length-parameter, but the real length of the data in the std::string-Buffer, it messes the Unicode-String up with characters that definetly not appeared at that position in the original string...

Do I have to write my own conversion function? Or maybe shall i keep the data as a casual char-array, because the special-symbols will be converted incorrectly?

With regards

share|improve this question
I'm a bit puzzled about what you're trying to do. Are you saying that you have the raw bits from a bitmap (image) file stored in a std::string? If so, then you probably DON'T want to do this, as the data that you have is not string (character/glpyh) data. The MBCS->UNICODE conversion simply doesn't make sense here. What is the larger problem you are trying to solve? –  Bukes Aug 23 '12 at 21:38
I wrote a function to send data over a socket. That function takes a std::wstring as a parameter... If i dont get the conversion, i need to rewrite this function to take a char* and the length of the buffer or something like this, which i dont really want... but if thats anything thats left to me... –  Incubbus Aug 23 '12 at 21:46
Rewrite the function, it is completely unsuitable to send binary data. –  Hans Passant Aug 23 '12 at 21:49
thx... plz post it as answer, so i can rate it –  Incubbus Aug 23 '12 at 21:52
@Incubbus - Your "function to send data over a socket" needs to be reworked. As your question is tagged C++, consider providing overloads that support properly marhsalling string, wstring, and binary data. –  Bukes Aug 23 '12 at 21:53
show 1 more comment

1 Answer

up vote 1 down vote accepted

There are many, many things that will fail with this approach. Among other things, extra bytes may be added to your data without your realizing it.

It's odd that your only option takes a std::wstring(). If this is a home-grown library, you should take the trouble to write a new function. If it's not, make sure there's nothing more suitable before writing your own.

share|improve this answer
add comment

Your Answer


