Valentyn Zakharenko
Valentyn Zakharenko

Reputation: 3088

How to convert byte array to union struct by one operation?

I declared in C# something like union in C++:

[StructLayout(LayoutKind.Explicit, Size = 5)]
public struct Marker
{
        [FieldOffset(0)] public byte label;
        [FieldOffset(1)] public int count;

        [FieldOffset(1)] private byte count_0;
        [FieldOffset(2)] private byte count_1;
        [FieldOffset(3)] private byte count_2;
        [FieldOffset(4)] private byte count_3;
}

Also I have byte[] bytes with size 5. I need to convert my array to Marker object. I can do it by following way:

var marker = new Marker 
{
    label = bytes[0],
    count = BitConverter.ToInt32(bytes, 1)
}

Or:

var marker = new Marker 
{
    label = bytes[0],
    count_0 = bytes[1],
    count_1 = bytes[2],
    count_2 = bytes[3],
    count_3 = bytes[4]
}

That's OK, but I think it is possible to do by more optimal way from performance view - just point marker to first byte of bytes. I trying to find something like this:

BitConverter.To<Marker>(bytes);

How to convert byte array to union struct by one operation?

Upvotes: 0

Views: 771

Answers (2)

Pepernoot
Pepernoot

Reputation: 3609

This should work

    static void Main(string[] args)
    {
        //Creating test data
        List<byte> data = new List<byte>();
        data.Add(123);
        data.AddRange(BitConverter.GetBytes((int)123456));

        //Converting test data to Struct
        Marker m = StructFromBytes<Marker>(data.ToArray());

        //Check if it works
        Console.WriteLine(m.label); //Prints 123
        Console.WriteLine(m.count); //Prints 123456
    }

    private static T StructFromBytes<T>(byte[] bytes)
    {
        int structSize = Marshal.SizeOf(typeof(T));
        byte[] structBytes = new byte[structSize];
        Array.Copy(bytes, 0, structBytes, 0, structSize);

        GCHandle handle = GCHandle.Alloc(structBytes, GCHandleType.Pinned);
        T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
        handle.Free();

        return theStructure;
    }

Upvotes: 1

Sefe
Sefe

Reputation: 14007

You can create a constructor:

[StructLayout(LayoutKind.Explicit, Size = 5)]
public struct Marker
{
    [FieldOffset(0)] public byte label;
    [FieldOffset(1)] public int count;

    [FieldOffset(1)] private byte count_0;
    [FieldOffset(2)] private byte count_1;
    [FieldOffset(3)] private byte count_2;
    [FieldOffset(4)] private byte count_3;

    public Marker(byte[] bytes)
    {
       label = bytes[0];
       count_0 = bytes[1];
       count_1 = bytes[2];
       count_2 = bytes[3];
       count_3 = bytes[4];
    }
}

Or an operator overload:

[StructLayout(LayoutKind.Explicit, Size = 5)]
public struct Marker
{
    [FieldOffset(0)] public byte label;
    [FieldOffset(1)] public int count;

    [FieldOffset(1)] private byte count_0;
    [FieldOffset(2)] private byte count_1;
    [FieldOffset(3)] private byte count_2;
    [FieldOffset(4)] private byte count_3;

    public static explicit operator Marker(byte[] bytes)
    {
       Marker result = new Marker();
       result.label = bytes[0];
       result.count_0 = bytes[1];
       result.count_1 = bytes[2];
       result.count_2 = bytes[3];
       result.count_3 = bytes[4];
       return result;
    }
}

Both solutions would make creating your struct a one-liner:

Marker marker = new Marker(bytes);
Marker marker = (Marker)bytes;

Upvotes: 0

Related Questions