gpt4 book ai didi

c# - .net 中的转换 : Native Utf-8 <-> Managed String

转载 作者:可可西里 更新时间:2023-11-01 08:53:23 24 4
gpt4 key购买 nike

我创建了这两种方法来将 native utf-8 字符串 (char*) 转换为托管字符串,反之亦然。以下代码完成这项工作:

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

虽然 NativeUtf8FromString 没问题,但 StringFromNativeUtf8 是一团糟,但这是我可以运行的唯一安全代码。使用不安全代码我可以使用字节*,但我不想要不安全代码。有没有其他人可以想到我不必为每个包含的字节复制字符串来找到 0 终止的地方。


我只是在这里添加取消保存的代码:

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

如您所见,它并不丑陋,只是需要不安全。

最佳答案

只需执行与 strlen() 执行的操作完全相同的操作。请考虑保留缓冲区,代码确实会很快产生垃圾。

    public static IntPtr NativeUtf8FromString(string managedString) {
int len = Encoding.UTF8.GetByteCount(managedString);
byte[] buffer = new byte[len + 1];
Encoding.UTF8.GetBytes(managedString, 0, managedString.Length, buffer, 0);
IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
return nativeUtf8;
}

public static string StringFromNativeUtf8(IntPtr nativeUtf8) {
int len = 0;
while (Marshal.ReadByte(nativeUtf8, len) != 0) ++len;
byte[] buffer = new byte[len];
Marshal.Copy(nativeUtf8, buffer, 0, buffer.Length);
return Encoding.UTF8.GetString(buffer);
}

关于c# - .net 中的转换 : Native Utf-8 <-> Managed String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com