0

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

3 Answers 3

3
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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would try something like this...

Encoding.ASCII.GetString(value, 9, value.Length - 9);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.