3

I want to convert a std::string to std::wstring. There are two approaches which i have come across.

  1. Given a string str we cant convert into wide string using the following code wstring widestring = std::wstring(str.begin(),str.end());

  2. The other approach is to use MultiByteToWideCharArray().

What i wanted to understand was what is the drawback of using the first approach and how does the second approach solves thing problem

skaffman
  • 398,947
  • 96
  • 818
  • 769
Siddharth Sarda
  • 486
  • 2
  • 7

2 Answers2

4

MultiByteToWideChar offers more options(like the ability to select "codepages") and translates non-standard symbols correctly

P47RICK
  • 136
  • 2
4

The first option doesn't support multibyte encoding. It will iterate through each byte (char) in the string and convert it to a wide character. When you have a string with multibyte encoding, individual characters can take more than one byte, so a standard string iterator is inappropriate.

The MultiByteToWideChar function has support for different multibyte formats, as specified by the codepage parameter.

Gunslinger47
  • 7,001
  • 2
  • 21
  • 29