[英]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::value
是char *
。
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.