【问题标题】:Convert chinese error message to String^ in managed C++在托管 C++ 中将中文错误消息转换为 String^
【发布时间】:2019-01-14 00:14:16
【问题描述】:

我有一个来自一家名为 CTP 的公司的 dll,它允许我在中国交易所进行交易。它在 C++ 中,而我的应用程序在 C# 中......我已经能够编写使用托管 C++ 链接到 dll 并将字符串 (ascii) 和其他参数从 C# 到 dll 和从 dll 转换的代码。唯一的问题是当我从 dll 中收到错误消息时,它们是中文的。而且我无法将返回的 char* 转换为包含良好中文字符的有效 String^。

例如下面的 char* 消息数组:

43 54 50 3A CE DE B4 CB C8 A8 CF DE 00

使用此代码:

return gcnew String^(message);

我收到“CTP:ÎÞ´ËȨÏÞ”。

使用此代码:

int bufSize = MultiByteToWideChar(CP_UTF8, 0, message, -1, NULL, 0);
wchar_t* wstr = new wchar_t[bufSize];
MultiByteToWideChar(CP_UTF8, 0, message, -1, wstr, bufSize);
String^ val = gcnew String(wstr);
delete[] wstr;
return val;

我得到“CTP:�……�����”。

此外,当我尝试通过在线十六进制 -> UTF8 转换器运行该文本字符串时,我收到一条错误消息,指出 utf8 字符串无效。

我很确定前四个字符是“CTP:”,因为这是编写 dll 的公司的名称。

但是,我无法弄清楚后面的汉字是如何编码的。有什么想法吗?

【问题讨论】:

    标签: c# character-encoding managed-c++ chinese-locale


    【解决方案1】:

    好的...所以编码是 GB18030 或“代码页”54936...所以下面的代码正确地转换了字符串。不确定是否有 GB18030 的#define。

    System::String^ CTPTraderWrapper::GetString(char* message)
    {
        int bufSize = MultiByteToWideChar(54936, 0, message, -1, NULL, 0);
        wchar_t* wstr = new wchar_t[bufSize];
        MultiByteToWideChar(54936, 0, message, -1, wstr, bufSize);
        String^ val = gcnew String(wstr);
        delete[] wstr;
        return val;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多