1

I've been programming many years but am very new to the the std namespace and the std::string class.

I'm writing code to read the value from Gdiplus::PropertyItem::value, which is char *.

What is the most accepted way to convert this char * value to string, which in my case is a Unicode string?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466

1 Answers1

4

You are mentioning string but you say it's a Unicode string. So then I suppose you mean wstring. You could use the MultiByteToWideChar function to convert between the two. Something like this:

std::string str(...);
int size = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstr(size, 0 );
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstr[0], size);
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • Sorry, I'm used to MFC and `CString`, which is ASCII or Unicode depending on build settings. So, yes, it looks like I need `std::wstring`. But I'm blown away that I need to use raw Windows API for this and that `std::wstring` doesn't offer even a helper here. – Jonathan Wood Mar 19 '14 at 07:28
  • @JonathanWood wstring vs string. Have a look http://stackoverflow.com/a/402918/1866301 – vaibhav kumar Mar 19 '14 at 07:31