将const char *转换为wstring [英] Convert const char* to wstring
问题描述
我正在开发一个基于锌的闪存应用程序的本机扩展,我需要将 const char *
转换为 wstring
这是我的代码:
mdmVariant_t * appendHexDataToFile(const zinc4CallInfo_t * pCallInfo,int paramCount,mdmVariant_t ** params){
if(paramCount> = 2){
const char * file = mdmVariantGetString(params [0] );
const char * data = mdmVariantGetString(params [1]);
return mdmVariantNewInt(native.AppendHexDataToFile(file,data));
}
else {
return mdmVariantNewBoolean(FALSE);
}
}
但 native.AppendHexDataToFile )
需要两个 wstring
。
我对C ++不是很好,我认为所有不同的字符串类型是完全混乱,我没有找到一些有用的网。
我建议您使用 std :: string
而不是C风格的字符串( char *
)。你可以通过简单地将它传递给它的构造函数从 const char *
创建 std :: string
一旦你有 std :: string
,就可以创建一个简单的函数来转换 std :: string包含多字节UTF-8字符到
std :: wstring
包含UTF-16编码点(16位表示的特殊字符从 std :: string
)。
有更多的方法可以这样做,下面是使用 MultiByteToWideChar函数:
std :: wstring s2ws(const std :: string& str)
{
int size_needed = MultiByteToWideChar(CP_UTF8,0,& str [0], )str.size(),NULL,0);
std :: wstring wstrTo(size_needed,0);
MultiByteToWideChar(CP_UTF8,0,& str [0],(int)str.size(),& wstrTo [0],size_needed);
return wstrTo;请检查这些问题:
}
< http://stackoverflow.com/q/6107776/1168156\">将多字节字符映射到其Unicode代码点
为什么要使用MultiByteToWideCharArray将std :: string转换为std :: wstring?
I'm working on a native extension for a zinc based flash application and I need to convert a const char*
to a wstring
.
This is my code:
mdmVariant_t* appendHexDataToFile(const zinc4CallInfo_t *pCallInfo, int paramCount, mdmVariant_t **params) {
if(paramCount >= 2) {
const char *file = mdmVariantGetString(params[0]);
const char *data = mdmVariantGetString(params[1]);
return mdmVariantNewInt(native.AppendHexDataToFile(file, data));
}
else {
return mdmVariantNewBoolean(FALSE);
}
}
But native.AppendHexDataToFile()
needs two wstring
.
I'm not very good with C++ and I think all those different string types are totally confusing and I didn't find something useful in the net. So I'm asking you guys how to do it.
Edit: The Strings are UTF-8 and I'm using OSX and Windows XP/Vista/7
I recommend you using std::string
instead of C-style strings (char*
) wherever possible. You can create std::string
object from const char*
by simple passing it to its constructor.
Once you have std::string
, you can create simple function that will convert std::string
containing multi-byte UTF-8 characters to std::wstring
containing UTF-16 encoded points (16bit representation of special characters from std::string
).
There are more ways how to do that, here's the way by using MultiByteToWideChar function:
std::wstring s2ws(const std::string& str)
{
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo( size_needed, 0 );
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
Check these questions too:
Mapping multibyte characters to their unicode point representation
Why use MultiByteToWideCharArray to convert std::string to std::wstring?
这篇关于将const char *转换为wstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!