0

So I have string variable that I want to convert to wstring.

I'm using mbstowcs_s.

Unfortunately my string variable contain \0 as a character (does not mark the end of the string) and has documented:

The mbstowcs_s function converts a string of multibyte characters pointed to by mbstr into wide characters stored in the buffer pointed to by wcstr. The conversion will continue for each character until one of these conditions is met: A multibyte null character is encountered

My temporary solution is to convert char by char. that solves the issue but unfortunately deteriorates performance drastically.

Any idea how to overcome that? must I use different function for conversion?

MrTux
  • 32,350
  • 30
  • 109
  • 146
idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • 1
    [Maybe useful](http://stackoverflow.com/a/25485477/596781) – Kerrek SB Oct 26 '14 at 11:42
  • Please do not post [duplicates of your own questions](http://stackoverflow.com/questions/26571564/mbstowcs-s-stops-conversion-if-multibyte-null-character-is-encountered) – arx Oct 26 '14 at 14:35

1 Answers1

1

You could use MultiByteToWideChar which allows you to specify the number of chars to convert.

int MultiByteToWideChar(
  _In_       UINT CodePage,
  _In_       DWORD dwFlags,
  _In_       LPCSTR lpMultiByteStr,
  _In_       int cbMultiByte,
  _Out_opt_  LPWSTR lpWideCharStr,
  _In_       int cchWideChar
);

See MultiByteToWideChar function.

However, MultiByteToWideChar is Win32 API, but that should be no problem in your case.

IIRC mbstowcs also calls MultiByteToWideChar.

idanshmu
  • 5,061
  • 6
  • 46
  • 92
MrTux
  • 32,350
  • 30
  • 109
  • 146
  • if `mbstowcs` also calls `MultiByteToWideChar`, do they deliberately call `MultiByteToWideChar` in such way the they stop on `\0`? – idanshmu Oct 26 '14 at 11:52
  • 1
    If you call `MultiByteToWideChar` with cbMultiByte=-1 it will also stop on `\0` and that's what `mbstowcs` might do. – MrTux Oct 26 '14 at 11:55