Home >Backend Development >C++ >How to Efficiently Convert a C/C Byte Array to a C# Structure?

How to Efficiently Convert a C/C Byte Array to a C# Structure?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-19 06:12:111047browse

How to Efficiently Convert a C/C   Byte Array to a C# Structure?

Read C/C data structure from byte array to C#

Question

This task involves converting a byte array containing data from a C/C structure into the corresponding C# structure. The C/C structure looks like this:

<code class="language-c++">typedef struct OldStuff {
    CHAR Name[8];
    UInt32 User;
    CHAR Location[8];
    UInt32 TimeStamp;
    UInt32 Sequence;
    CHAR Tracking[16];
    CHAR Filler[12];
} OldStuff;</code>

The C# structure, named NewStuff, is defined as follows:

<code class="language-csharp">[StructLayout(LayoutKind.Explicit, Size = 56, Pack = 1)]
public struct NewStuff
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
    [FieldOffset(0)]
    public string Name;

    [MarshalAs(UnmanagedType.U4)]
    [FieldOffset(8)]
    public uint User;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
    [FieldOffset(12)]
    public string Location;

    [MarshalAs(UnmanagedType.U4)]
    [FieldOffset(20)]
    public uint TimeStamp;

    [MarshalAs(UnmanagedType.U4)]
    [FieldOffset(24)]
    public uint Sequence;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
    [FieldOffset(28)]
    public string Tracking;
}</code>

Proposed solution

Initially, a more cumbersome approach was considered, involving pinned memory and using Marshal.PtrToStructure:

<code class="language-csharp">int BufferSize = Marshal.SizeOf(typeof(NewStuff));
byte[] buff = new byte[BufferSize];

Array.Copy(SomeByteArray, 0, buff, 0, BufferSize);

handle = GCHandle.Alloc(buff, GCHandleType.Pinned);

MyStuff = (NewStuff)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(NewStuff));

handle.Free();</code>

Improvements

After further analysis, it was determined that the buffer copy in the original method was unnecessary. Instead, direct handle pinning is sufficient:

<code class="language-csharp">GCHandle handle;
NewStuff MyStuff;

handle = GCHandle.Alloc(SomeByteArray, GCHandleType.Pinned);
try
{
    MyStuff = (NewStuff)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(NewStuff));
}
finally
{
    handle.Free();
}</code>

Also, an even simpler version can be used using generics (requires unsafe switching):

  • Generic:
<code class="language-csharp">T ByteArrayToStructure<T>(byte[] bytes) where T : struct
{
    T stuff;
    GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
    try
    {
        stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    }
    finally
    {
        handle.Free();
    }
    return stuff;
}</code>
  • Easier (unsafe):
<code class="language-csharp">unsafe T ByteArrayToStructure<T>(byte[] bytes) where T : struct
{
    fixed (byte* ptr = &bytes[0])
    {
        return (T)Marshal.PtrToStructure((IntPtr)ptr, typeof(T));
    }
}</code>

Use BinaryReader to improve performance

While the preferred solution involves fixation and Marshal.PtrToStructure, in certain cases parsing data using the BinaryReader class may provide performance benefits. However, specific implementations must be evaluated to determine whether such gains are significant.

The above is the detailed content of How to Efficiently Convert a C/C Byte Array to a C# Structure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn