Reputation: 1658
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?
Upvotes: 1
Views: 1503
Reputation: 1658
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.
Upvotes: 2