单声道和传递unicode字符串

mono and passing on unicode strings

本文关键字:unicode 字符串 声道 单声道      更新时间:2023-10-16

我在我的应用程序中嵌入mono,但我有mono的字符串转换问题。

c++代码:

static inline void p_Print(MonoString *str) {
    cout << "UTF8: " << mono_string_to_utf8(str) << endl;
    wcout << "UTF16: " << ((wchar_t*)mono_string_to_utf16(str)) << endl;
}
//...
mono_add_internal_call("SampSharp.GameMode.Natives.Native::Print", (void *)p_Print);
c#代码:

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void Print(string msg);
//...
Print("characters like u00D6 are working? (should be an O with " above it)");
输出:

UTF8: characters like Ö are working? (should be an O with " above it)
UTF16: characters like Í are working? (should be an O with " above it)

正如您所看到的,输出不完全是它应该打印的,它应该打印"像Ö这样的字符正在工作?"(应该是一个带有" above it "的O)",但是mono_string_to_utf8和_to_utf16都没有做它应该做的事情。

如何解决这个问题?

解决方法如下:

string mono_string_to_string(MonoString *str)
{
    mono_unichar2 *chl = mono_string_chars(str);
    string out("");
    for (int i = 0; i < mono_string_length(str); i++) {
        out += chl[i];
    }
    return out;
}

可能不是最漂亮的方式,但它有效。