I am getting String^
which Contains some Indian language characters in a callback from C# Component in my C++ WinRT Component in a Cocos2dx game for Windows Phone 8 project.
Whenever I convert it to std::string
the Hindi and other characters turn in to garbage characters. I'm not able to find why this is happening.
Here is a sample code and I have just defined Platform::String^
here but consider it's passed to C++ WinRT Component
from C# Component
String^ str = L"विकास, વિકાસ, ਵਿਕਾਸ, Vikas";
std::wstring wsstr(str->Data());
std::string res(wsstr.begin(), wsstr.end());
With C++, you can convert from
Platform::String
tostd::string
with the following code:Reference: How to convert Platform::String to char*?
Edit: see this answer for a better portable solution.
The problem is that
std::string
only holds 8-bit character data and yourPlatform::String^
holds Unicode data. Windows provides functionsWideCharToMultiByte
andMultiByteToWideChar
to convert back and forth: