代码之家  ›  专栏  ›  技术社区  ›  PassionateDeveloper

cfstring到string?

  •  0
  • PassionateDeveloper  · 技术社区  · 15 年前

    我有以下代码将字符串转换为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 .net 2)

    3 回复  |  直到 12 年前
        1
  •  1
  •   Flesrouy    15 年前

    我想试试这个…

    Encoding.ASCII.GetString(value, 9, value.Length - 9);
    
        2
  •  3
  •   WebMonster    13 年前
    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);
    }
    
        3
  •  0
  •   pizixie    12 年前

    这个代码是错误的 当参数“value”为utf8字符串时 比如中文。

    如果需要cfstring返回字符串

    请将corefundation.cfstring与构造函数cfstring(intptr ptr)一起使用

    参数“ptr”是指向cfstring的指针。

    corefundation.cfstring-> https://github.com/isnowrain/CoreFoundation