开发者

converting between struct and byte array

his question is about converting between a struct and a byte array. Many solutions are based around GCHandle.Alloc()开发者_如何学编程 and Marshal.StructureToPtr(). The problem is these calls generate garbage. For example, under Windows CE 6 R3 about 400 bytes of garbarge is made with a small structure. If the code below could be made to work the solution could be considered cleaner. It appears the sizeof() happens too late in the compile to work.

public struct Data
{
    public double a;
    public int b;
    public double c;
}

[StructLayout(LayoutKind.Explicit)]
public unsafe struct DataWrapper
{
    private static readonly int val = sizeof(Data);

    [FieldOffset(0)]
    public fixed byte Arr[val]; // "fixed" is to embed array instead of ref

    [FieldOffset(0)]
    public Data; // based on a C++ union
}


You can in fact use the constant value 1 as the size of the fixed array in DataWrapper. There is no bounds checking when accessing an unsafe fixed array nor is there any mechanism to request its length at runtime. The presence of the Data member at offset 0 is enough to ensure that you can actually read and write sizeof(Data) bytes to/from DataWrapper.Arr.

With that said, a more straight forward solution would be to cast a Data* pointer to byte* as follows:

unsafe {
    Data d = new Data();
    d.a = 125.5;
    d.b = 0x7FEEDDEE;
    d.c = 130.0;
    byte* bytes = (byte*)&d;
    for (int i = 0; i < sizeof(Data); i++) {
      Console.WriteLine(i + ": " + bytes[i]);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜