I created those two methods to convert Native utf-8 strings (char*) into managed string and vice versa. The following code does the job:
public IntPtr NativeUtf8FromString(string managedString)
{
byte[] buffer = Encoding.UTF8.GetBytes(managedString); // not null terminated
Array.Resize(ref buffer, buffer.Length + 1);
buffer[buffer.Length - 1] = 0; // terminating 0
IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
return nativeUtf8;
}
string StringFromNativeUtf8(IntPtr nativeUtf8)
{
int size = 0;
byte[] buffer = {};
do
{
++size;
Array.Resize(ref buffer, size);
Marshal.Copy(nativeUtf8, buffer, 0, size);
} while (buffer[size - 1] != 0); // till 0 termination found
if (1 == size)
{
return ""; // empty string
}
Array.Resize(ref buffer, size - 1); // remove terminating 0
return Encoding.UTF8.GetString(buffer);
}
While NativeUtf8FromString is ok, StringFromNativeUtf8 is a mess but the only safe code I could get to run. Using unsafe code I could use an byte* but I do not want unsafe code. Is there another way someone can think of where I do not have to copy the string for every contained byte to find the 0 termination.
I just add the unsave code here:
public unsafe string StringFromNativeUtf8(IntPtr nativeUtf8)
{
byte* bytes = (byte*)nativeUtf8.ToPointer();
int size = 0;
while (bytes[size] != 0)
{
++size;
}
byte[] buffer = new byte[size];
Marshal.Copy((IntPtr)nativeUtf8, buffer, 0, size);
return Encoding.UTF8.GetString(buffer);
}
As you see its not ugly just needs unsafe.
unsafe
code? – Decillion/unsafe
switch is pretty meaningless.Marshal.*
is just as unsafe as pointer code, even if it doesn't require the switch. – DecillionMarshal.Copy
allows you to write data to arbitrary memory locations, just like pointers allow you to write data to arbitrary memory locations. No difference in the damage you can do. – Decillion