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::stringinstead of C-style strings (char*) wherever possible. You can createstd::stringobject fromconst char*by simple passing it to its constructor.Once you have
std::string, you can create simple function that will convertstd::stringcontaining multi-byte UTF-8 characters tostd::wstringcontaining UTF-16 encoded points (16bit representation of special characters fromstd::string).There are more ways how to do that, here’s the way by using MultiByteToWideChar function:
Check these questions too:
Mapping multibyte characters to their unicode point representation
Why use MultiByteToWideCharArray to convert std::string to std::wstring?