Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写C#CustomMarshaler从字节数组中获取以null结尾的ANSI字符串?_C#_Arrays_Marshalling - Fatal编程技术网

如何编写C#CustomMarshaler从字节数组中获取以null结尾的ANSI字符串?

如何编写C#CustomMarshaler从字节数组中获取以null结尾的ANSI字符串?,c#,arrays,marshalling,C#,Arrays,Marshalling,如何编写C#CustomMarshaler从字节数组中获取以null结尾的ANSI字符串 我有很多二进制文件需要解析成结构。我所看到的最好且干净的方法是使用MarshalAs属性装饰结构,并使用Marshal.PtrToStructure() 但我遇到了一个问题。字符串的默认封送处理不适用于我 字节数组中的字符串以null结尾,每个字符一个字节(不是指针,也不是COM样式) 我使用此方法将字节数组封送到结构: public static T MarshalStruct<T>(byte

如何编写C#CustomMarshaler从字节数组中获取以null结尾的ANSI字符串

我有很多二进制文件需要解析成结构。我所看到的最好且干净的方法是使用MarshalAs属性装饰结构,并使用Marshal.PtrToStructure()

但我遇到了一个问题。字符串的默认封送处理不适用于我

字节数组中的字符串以null结尾,每个字符一个字节(不是指针,也不是COM样式)

我使用此方法将字节数组封送到结构:

public static T MarshalStruct<T>(byte[] data) where T : struct
{
    GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
    T temp = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    handle.Free();
    return temp;
}
公共静态T marshallstruct(字节[]数据),其中T:struct
{
GCHandle=GCHandle.Alloc(数据,GCHandleType.pinted);
T temp=(T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T));
handle.Free();
返回温度;
}
这是我的字符串自定义封送拆收器: 公共类AnsiNullTerminatedString:ICustomMarshaler {

    public object MarshalNativeToManaged(IntPtr pointerNativeData)
    {
        List<byte> bytes = new List<byte>();
        do
        {
            bytes.Add(Marshal.ReadByte(pointerNativeData, bytes.Count));
        } while (bytes[bytes.Count - 1] != 0);

        return Encoding.UTF8.GetString(bytes.ToArray());
    }
}
公共对象MarshallNativeToManager(IntPtr pointerNativeData)
{
列表字节=新列表();
做
{
Add(Marshal.ReadByte(pointerNativeData,bytes.Count));
}while(bytes[bytes.Count-1]!=0);
返回Encoding.UTF8.GetString(bytes.ToArray());
}
}
但它的压榨


有没有一种方法可以为此类字符串编写正确的自定义封送拆收器,从而在不使用手动封送或二进制读取器的情况下解决此问题?

“但是它的崩溃”不是一个合适的问题描述。不能对数组使用SizeParamIndex,它必须是SizeConst。若要调试代码,请从一个字段开始。C语言字符串是以“\0”结尾的bye[]数组;因此,您希望定位“\0”,并在GetStrings()中仅获取适当的字节数。您可能会在字符串末尾收到很多垃圾。
    public object MarshalNativeToManaged(IntPtr pointerNativeData)
    {
        List<byte> bytes = new List<byte>();
        do
        {
            bytes.Add(Marshal.ReadByte(pointerNativeData, bytes.Count));
        } while (bytes[bytes.Count - 1] != 0);

        return Encoding.UTF8.GetString(bytes.ToArray());
    }
}