简体   繁体   中英

Convert char* to Unicode std::string

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM