I have follow code to make a String to a 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开发者_JAVA技巧 static byte[] StringToCString(string value)
{
byte[] bytes = new byte[value.Length + 1];
Encoding.ASCII.GetBytes(value, 0, value.Length, bytes, 0);
return bytes;
}
How I can get a CFString to String back? (C#.Net 2.0)
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);
}
I would try something like this...
Encoding.ASCII.GetString(value, 9, value.Length - 9);
this code is error when the parameter "value" is utf8 string such as Chinese.
if you need CFString to String back
please use the CoreFoundation.CFString with constructor CFString(IntPtr ptr)
the parameter "ptr" is a pointer to a CFString.
CoreFoundation.CFString -> https://github.com/isnowrain/CoreFoundation
精彩评论