我使用以下代码将字符串转换为CFString:
internal static byte[] StringToCFString(string value)
{
byte[] b;
b = new byte[value.Length + 10];
b[4] = 0x8c;
b[5] = 07;
b[6] = 01;
b[8] = (byte)value.Length;
Encoding.ASCII.GetBytes(value, 0, value.Length, b, 9);
return b;
}
public static byte[] StringToCString(string value)
{
byte[] bytes = new byte[value.Length + 1];
Encoding.ASCII.GetBytes(value, 0, value.Length, bytes, 0);
return bytes;
}怎样才能让CFString返回字符串?(C#.Net 2.0)
发布于 2010-07-15 03:26:17
我会尝试像这样的东西。
Encoding.ASCII.GetString(value, 9, value.Length - 9);发布于 2012-10-17 05:53:15
private String CFString2String(IntPtr intPtr)
{
byte size = Marshal.ReadByte(intPtr, 8);
byte[] b = new byte[size];
Marshal.Copy(IntPtr.Add(intPtr, 9), b, 0, size);
return Encoding.ASCII.GetString(b);
}发布于 2014-03-01 00:58:40
当参数值为中文等utf8字符串时,该代码错误。
如果您需要使用CFString返回字符串
请使用带有构造函数CFString(IntPtr ptr)的CoreFoundation.CFString
参数"ptr“是指向CFString的指针。
CoreFoundation.CFString -> https://github.com/isnowrain/CoreFoundation
https://stackoverflow.com/questions/3249503
复制相似问题