c# - 单声道并传递 unicode 字符串

标签 c# c++ unicode 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)

如您所见,输出不完全是它应该打印的内容,它应该打印“像 Ö 这样的字符在工作吗?(应该是上面有“的 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;
}

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

关于c# - 单声道并传递 unicode 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24367603/

相关文章:

c++ - 在 std::max 和 min 中使用静态类数据

C++: "error: expected class-name before ‘{’ token"继承模板类时

python - 具有适当 unicode 支持的图表

unicode - 如何在 Julia 中加载 UTF16 编码的文本文件?

python - 如何在 python 3 中生成希伯来语字符串?

c# - 拥有兼容的接口(interface),无需继承

c# - 通用 TypeCode 类型检查?

c# - C++ C# 项目依赖管理

c# - 在 Windows 7 中更改屏幕保护程序设置时应用程序卡住 - System.Threading.Timer 是罪魁祸首?

c++ - 在 Eigen::Matrix::data() 上使用 std::move 将 Eigen::Matrix 转换为 vector