简体   繁体   English

将char *转换为Unicode std :: string

[英]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. 我已经编程很多年了,但对std名称空间和std::string类来说却是新手。

I'm writing code to read the value from Gdiplus::PropertyItem::value , which is char * . 我正在编写代码以读取Gdiplus::PropertyItem::value ,该Gdiplus::PropertyItem::valuechar *

What is the most accepted way to convert this char * value to string , which in my case is a Unicode string? 将这个char *值转换为string ,最常见的方法是什么(在我的情况下是Unicode字符串)?

You are mentioning string but you say it's a Unicode string. 您提到的是string但您说的是Unicode字符串。 So then I suppose you mean wstring . 所以,我想你的意思是wstring You could use the MultiByteToWideChar function to convert between the two. 您可以使用MultiByteToWideChar函数在两者之间进行转换。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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