简体   繁体   中英

mono and passing on unicode strings

I'm embedding mono in an application of mine but i have issues with mono's string conversion.

C++ code:

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# code:

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void Print(string msg);

//...

Print("characters like \u00D6 are working? (should be an O with \" above it)");

output:

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

As you can see, the output is not quite what it should print, it should be printing "characters like Ö are working? (should be an O with " above it)", but neither mono_string_to_utf8 or _to_utf16 does what it should do.

how can i solve this issue?

Solved it as follows:

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

Might not be the most beautiful way of doing it, but it works.

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