from byte[] to structure

L

Lars Wilhelmsen

Hi Ben,

Ben Terry said:
Can anyone tell me how to get data from a byte array into the following
structure?

Feel free to use this method:

public static object RawDeserialize( byte[] rawData, int position, Type
anyType )
{
int rawsize = Marshal.SizeOf( anyType );
if( rawsize > rawData.Length )
return null;
IntPtr buffer = Marshal.AllocHGlobal( rawsize );
Marshal.Copy( rawData, position, buffer, rawsize );
object retobj = Marshal.PtrToStructure( buffer, anyType );
Marshal.FreeHGlobal( buffer );
return retobj;
}

the position is the position into the byte array to start deserializing
from, and the
type is the type of the structure - typof(MESSAGE_LOG_HEADER_STRUCT2).
Remember to cast the return value to the same type too.

And if you want to do vice versa, use this one:

public static byte[] RawSerialize( object anything )
{
int rawSize = Marshal.SizeOf( anything );
IntPtr buffer = Marshal.AllocHGlobal( rawSize );
Marshal.StructureToPtr( anything, buffer, false );
byte[] rawDatas = new byte[ rawSize ];
Marshal.Copy( buffer, rawDatas, 0, rawSize );
Marshal.FreeHGlobal( buffer );
return rawDatas;
}
 
B

BMermuys

Hi,


public YourStruct BytesToStruct(byte[] dataIn)
{
GCHandle hDataIn = GCHandle.Alloc(dataIn, GCHandleType.Pinned);
YourStruct ys =
(YourStruct)Marshal.PtrToStructure(hDataIn.AddrOfPinnedObject(),
typeof(YourStruct));
hDataIn.Free();
return ys;
}

HTH,
greetings
 
B

Ben Terry

Can anyone tell me how to get data from a byte array into the following
structure?

[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct MESSAGE_LOG_HEADER_STRUCT2
{
public DateTime dtTimeStamp;
public Int32 dSerialNumber;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public byte[] strMCFirmwareVersion;
public Int32 dMCFirmwareChecksum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public byte[] strSBCSoftwareVersion;
public Int32 dSBCSoftwareChecksum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
public byte[] strBarcode;
}

I have tried the following code, but I get the exception "BinaryFormatter
Version incompatibility." when it executes.

MemoryStream ms = new MemoryStream(bytes); // bytes is a byte[]
BinaryFormatter bf = new BinaryFormatter();
m_CompleteIllumStateMsg = (MIX_CompleteIllumStateMsg)bf.Deserialize(ms); //
m_CompleteIllumStateMsg is a MESSAGE_LOG_HEADER_STRUCT2
 
B

Ben Terry

Excellent! Thank you so much.

Lars Wilhelmsen said:
Hi Ben,

Ben Terry said:
Can anyone tell me how to get data from a byte array into the following
structure?

Feel free to use this method:

public static object RawDeserialize( byte[] rawData, int position, Type
anyType )
{
int rawsize = Marshal.SizeOf( anyType );
if( rawsize > rawData.Length )
return null;
IntPtr buffer = Marshal.AllocHGlobal( rawsize );
Marshal.Copy( rawData, position, buffer, rawsize );
object retobj = Marshal.PtrToStructure( buffer, anyType );
Marshal.FreeHGlobal( buffer );
return retobj;
}

the position is the position into the byte array to start deserializing
from, and the
type is the type of the structure - typof(MESSAGE_LOG_HEADER_STRUCT2).
Remember to cast the return value to the same type too.

And if you want to do vice versa, use this one:

public static byte[] RawSerialize( object anything )
{
int rawSize = Marshal.SizeOf( anything );
IntPtr buffer = Marshal.AllocHGlobal( rawSize );
Marshal.StructureToPtr( anything, buffer, false );
byte[] rawDatas = new byte[ rawSize ];
Marshal.Copy( buffer, rawDatas, 0, rawSize );
Marshal.FreeHGlobal( buffer );
return rawDatas;
}

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top