转换平台:: String要为std :: string [英] Convert Platform::String to std::string

查看:202
本文介绍了转换平台:: String要为std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到字符串^ 其中包含从C#组件在我的C ++的WinRT组件的回调在Cocos2dx游戏的Windows Phone 8项目的一些印度语字符。



每当我将它转换为的std ::字符串的印地文和其他字符转向到乱码。我无法找到为什么发生这种情况。



下面是一个示例代码,我刚才定义平台::字符串^ 在这里,但考虑它传递给 C ++的WinRT组件从C#组件

 字符串^海峡= Lविकास,વિકાસ,ਵਿਕਾਸ,维卡斯 
的std :: wstring的wsstr(STR-GT&;数据());
标准::字符串资源(wsstr.begin(),wsstr.end());


解决方案

编辑:看的this回答一个更好的便携式解决方案。



问题是,的std ::字符串仅持有8位字符数据和平台::字符串^ 保存Unicode数据。 Windows提供的功能调用WideCharToMultiByte 的MultiByteToWideChar 来来回转换:

 的std ::字符串make_string(常量的std :: wstring的&安培; wstring的)
{
自动wideData = wstring.c_str();
INT BUFFERSIZE =调用WideCharToMultiByte(CP_UTF8,0,wideData,-1,nullptr,0,NULL,NULL);
自动= UTF8的std :: make_unique<的char []>(缓冲区大小);
如果(0 ==调用WideCharToMultiByte(CP_UTF8,0,wideData,-1,utf8.get(),缓冲区大小,NULL,NULL))
抛出std ::异常(无法字符串转换为UTF8);

返回的std ::字符串(utf8.get());
}

的std :: wstring的make_wstring(常量标准::字符串&放大器;字符串)
{
自动utf8Data = string.c_str();
INT BUFFERSIZE =的MultiByteToWideChar(CP_UTF8,0,utf8Data,-1,nullptr,0);
自动宽=的std :: make_unique< wchar_t的[]>(缓冲区大小);
如果(0 ==的MultiByteToWideChar(CP_UTF8,0,utf8Data,-1,wide.get(),缓冲区大小))
抛出std ::例外(不能把字符串转换为Unicode);

返回的std :: wstring的(wide.get());
}

无效测试()
{
平台::字符串^海峡= Lविकास,વિકાસ,ਵਿਕਾਸ,维卡斯
的std :: wstring的wsstr(STR-GT&;数据());
自动utf8Str = make_string(wsstr); // UTF8编码的文本
wsstr = make_wstring(utf8Str); //一样原文
}


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());

解决方案

Edit: see this answer for a better portable solution.

The problem is that std::string only holds 8-bit character data and your Platform::String^ holds Unicode data. Windows provides functions WideCharToMultiByte and MultiByteToWideChar to convert back and forth:

std::string make_string(const std::wstring& wstring)
{
  auto wideData = wstring.c_str();
  int bufferSize = WideCharToMultiByte(CP_UTF8, 0, wideData, -1, nullptr, 0, NULL, NULL);
  auto utf8 = std::make_unique<char[]>(bufferSize);
  if (0 == WideCharToMultiByte(CP_UTF8, 0, wideData, -1, utf8.get(), bufferSize, NULL, NULL))
    throw std::exception("Can't convert string to UTF8");

  return std::string(utf8.get());
}

std::wstring make_wstring(const std::string& string)
{
  auto utf8Data = string.c_str();
  int bufferSize = MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, nullptr, 0);
  auto wide = std::make_unique<wchar_t[]>(bufferSize);
  if (0 == MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, wide.get(), bufferSize))
    throw std::exception("Can't convert string to Unicode");

  return std::wstring(wide.get());
}

void Test()
{
  Platform::String^ str = L"विकास, વિકાસ, ਵਿਕਾਸ, Vikas";
  std::wstring wsstr(str->Data());
  auto utf8Str = make_string(wsstr); // UTF8-encoded text
  wsstr = make_wstring(utf8Str); // same as original text
}

这篇关于转换平台:: String要为std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆