Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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#_C#_.net_Marshalling - Fatal编程技术网

编组短裤翻转顺序c#

编组短裤翻转顺序c#,c#,.net,marshalling,C#,.net,Marshalling,我有一个struct: [StructLayout(LayoutKind.Explicit)] private struct Test { [FieldOffset(0)] public readonly short Foo; [FieldOffset(2)] public readonly short Bar; [FieldOffset(4)] public readonly short Baz; } 和以下字节数组: var bytes

我有一个
struct

[StructLayout(LayoutKind.Explicit)]
private struct Test
{
    [FieldOffset(0)]
    public readonly short Foo;

    [FieldOffset(2)]
    public readonly short Bar;

    [FieldOffset(4)]
    public readonly short Baz;
}
和以下字节数组:

var bytes = new byte[]
{
    0x00, 0x01,
    0x00, 0x05,
    0xFF, 0xFB
};
我使用以下帮助函数将字节数组转换为结构:

private static T ByteArrayToStructure<T>(byte[] bytes)
    where T : struct
{
    GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
    try
    {
        return (T) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    }
    finally
    {
        handle.Free();
    }
}
私有静态T字节数组结构(字节[]字节)
其中T:struct
{
GCHandle=GCHandle.Alloc(字节,GCHandleType.pinted);
尝试
{
返回(T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T));
}
最后
{
handle.Free();
}
}
因此,我使用它如下:

var test = ByteArrayToStructure<Test>(bytes);

Assert.AreEqual(1, test.Foo);
Assert.AreEqual(5, test.Bar);
Assert.AreEqual(-5, test.Baz);
var test=ByteArrayToStructure(字节);
Assert.AreEqual(1,test.Foo);
断言相等(5,测试条);
Assert.AreEqual(-5,test.Baz);
现在看看这些断言,我们可以清楚地看到我所期望的,如果结果不是这样,这个问题就会出现

似乎有什么东西在翻转这2个字节,因此第一个字节是
0x0100
而不是
0x0001
,第二个字节是
0x0500
而不是
0x0005
,最后一个字节是
0xFBFF
而不是
0xFFFB

有没有办法禁用此行为


谢谢

如果要更改数据的结尾,这是不够的:

Array.Reverse(data);
Test t = ByteArrayToStructure<Test>(bytes);
只有知道结构中每个字段的大小时,才能反转数组的字节顺序,以便可以有选择地反转数组中各自的字节块。或者,在结构的每个字段从数组封送后,应该对其执行字节交换,如下所示:

Test t = ByteArrayToStructure<Test>(bytes);
t.Foo = SwapBytes(t.Foo);
t.Bar = SwapBytes(t.Bar);
t.Baz = SwapBytes(t.Baz);

private static short SwapBytes(short value)
{
    return (short)((value >> 8) | ((value & 0xFF) << 8));
}
Test t=ByteArrayToStructure(字节);
t、 Foo=交换字节(t.Foo);
t、 Bar=交换字节(t.Bar);
t、 Baz=交换体(t.Baz);
专用静态短交换字节(短值)
{

返回(短)((值>8)”((值和0xFF)。你的期望与现实脱节;考虑更新你的期望以匹配现实,然后失配将消失。你所看到的行为是正确的。如果你想调整这个字节数,那么要么调整它在字节数组末尾,要么写封隔器。没有“翻转”。,数组的字节值顺序错误。你的机器是little-endian,最后一台可以执行C#代码的big-endian机器是xbox-360。因此,必须先使用big-endian,0x01、0x00、0x05等。人类倾向于使用big-endian,但他们在与机器的战斗中失败了。历史。如果你真的想从big-endian开始数据,请参阅标记的复制,了解如何在编组后交换字节,以便获得预期的结果。如果只是因为您不了解大尾端和小尾端之间的区别,并且刚刚错误地初始化了
字节[]
,那么…只需正确初始化
字节[]
数组。
Test t = ByteArrayToStructure<Test>(bytes);
t.Foo = SwapBytes(t.Foo);
t.Bar = SwapBytes(t.Bar);
t.Baz = SwapBytes(t.Baz);

private static short SwapBytes(short value)
{
    return (short)((value >> 8) | ((value & 0xFF) << 8));
}