C#指针与字节数组的操作

private static byte[] ReadBytesFromPtr(IntPtr intPtr, int bufferLength)
{
    var result = new byte[bufferLength];
    var count = bufferLength;
    for (var i = 0; i < bufferLength; i++)
    {
        result[i] = Marshal.ReadByte(intPtr, i);
        if (result[i] == 0)
        {
            count = i;
            break;
        }
    }
    return result.Take(count).ToArray();
}
private static void WriteBytesToPtr(IntPtr intPtr, byte[] bytes)
{
    int j;
    for (j = 0; j < bytes.Length; j++)
    {
        Marshal.WriteByte(intPtr, j, bytes[j]);
    }
    Marshal.WriteByte(intPtr, j, 0);
}

 

posted on 2013-07-11 07:55  yao2yao4  阅读(4683)  评论(0编辑  收藏  举报

导航