简体   繁体   中英

Convert chinese error message to String^ in managed C++

I have a dll from a company called CTP that allows me to trade on Chinese exchanges. It is in C++ and my app is in C#... I have been able to write code that uses managed C++ to link to the dll and translate strings (ascii) and other parameters from C# to and from the dll. The only problem is when I get error messages back from the dll they are in Chinese. And I am having trouble translating the char* I get back into a valid String^ that contains good Chinese characters.

For example, the following char* message array:

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

using this code:

return gcnew String^(message);

I get "CTP:ÎÞ´ËȨÏÞ".

using this code:

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;

I get "CTP: ޴ Ȩ ".

Further, when I try to run that text string through online hex -> UTF8 converters I get an error message that the utf8 string is invalid.

I am very sure that the first four characters are "CTP:" as this is the name of the company that wrote the dll.

However, I cannot figure out how the Chinese characters that follow are encoded. Any ideas?

Ok... so the encoding is GB18030 or "code page" 54936... so the following code converted the string properly. Not sure if there is a #define for GB18030 or not.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM