from byte[] to structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Terry

    from byte[] to structure

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

    [Serializable]
    [StructLayout(La youtKind.Sequen tial)]
    public struct MESSAGE_LOG_HEA DER_STRUCT2
    {
    public DateTime dtTimeStamp;
    public Int32 dSerialNumber;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=8)]
    public byte[] strMCFirmwareVe rsion;
    public Int32 dMCFirmwareChec ksum;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=8)]
    public byte[] strSBCSoftwareV ersion;
    public Int32 dSBCSoftwareChe cksum;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=32)]
    public byte[] strBarcode;
    }

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

    MemoryStream ms = new MemoryStream(by tes); // bytes is a byte[]
    BinaryFormatter bf = new BinaryFormatter ();
    m_CompleteIllum StateMsg = (MIX_CompleteIl lumStateMsg)bf. Deserialize(ms) ; //
    m_CompleteIllum StateMsg is a MESSAGE_LOG_HEA DER_STRUCT2


  • Lars Wilhelmsen

    #2
    Re: from byte[] to structure

    Hi Ben,

    "Ben Terry" <bt@charter.net > wrote in message
    news:u7fE1kRaEH A.3752@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Can anyone tell me how to get data from a byte array into the following
    > structure?
    >[/color]

    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.AllocHG lobal( rawsize );
    Marshal.Copy( rawData, position, buffer, rawsize );
    object retobj = Marshal.PtrToSt ructure( buffer, anyType );
    Marshal.FreeHGl obal( 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_L OG_HEADER_STRUC T2).
    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.AllocHG lobal( rawSize );
    Marshal.Structu reToPtr( anything, buffer, false );
    byte[] rawDatas = new byte[ rawSize ];
    Marshal.Copy( buffer, rawDatas, 0, rawSize );
    Marshal.FreeHGl obal( buffer );
    return rawDatas;
    }

    --
    Lars Wilhelmsen

    Software Engineer
    Teleplan A/S, Norway


    Comment

    • Ben Terry

      #3
      Re: from byte[] to structure

      Excellent! Thank you so much.

      "Lars Wilhelmsen" <larswil@NOSPAM .ifi.uio.no> wrote in message
      news:uVgLbqRaEH A.996@TK2MSFTNG P12.phx.gbl...[color=blue]
      > Hi Ben,
      >
      > "Ben Terry" <bt@charter.net > wrote in message
      > news:u7fE1kRaEH A.3752@TK2MSFTN GP12.phx.gbl...[color=green]
      > > Can anyone tell me how to get data from a byte array into the following
      > > structure?
      > >[/color]
      >
      > 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.AllocHG lobal( rawsize );
      > Marshal.Copy( rawData, position, buffer, rawsize );
      > object retobj = Marshal.PtrToSt ructure( buffer, anyType );
      > Marshal.FreeHGl obal( 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_L OG_HEADER_STRUC T2).
      > 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.AllocHG lobal( rawSize );
      > Marshal.Structu reToPtr( anything, buffer, false );
      > byte[] rawDatas = new byte[ rawSize ];
      > Marshal.Copy( buffer, rawDatas, 0, rawSize );
      > Marshal.FreeHGl obal( buffer );
      > return rawDatas;
      > }
      >
      > --
      > Lars Wilhelmsen
      > http://www.sral.org/
      > Software Engineer
      > Teleplan A/S, Norway
      >
      >[/color]


      Comment

      • BMermuys

        #4
        Re: from byte[] to structure

        Hi,


        public YourStruct BytesToStruct(b yte[] dataIn)
        {
        GCHandle hDataIn = GCHandle.Alloc( dataIn, GCHandleType.Pi nned);
        YourStruct ys =
        (YourStruct)Mar shal.PtrToStruc ture(hDataIn.Ad drOfPinnedObjec t(),
        typeof(YourStru ct));
        hDataIn.Free();
        return ys;
        }

        HTH,
        greetings


        "Ben Terry" <bt@charter.net > wrote in message
        news:u7fE1kRaEH A.3752@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Can anyone tell me how to get data from a byte array into the following
        > structure?
        >
        > [Serializable]
        > [StructLayout(La youtKind.Sequen tial)]
        > public struct MESSAGE_LOG_HEA DER_STRUCT2
        > {
        > public DateTime dtTimeStamp;
        > public Int32 dSerialNumber;
        > [MarshalAs(Unman agedType.ByValA rray, SizeConst=8)]
        > public byte[] strMCFirmwareVe rsion;
        > public Int32 dMCFirmwareChec ksum;
        > [MarshalAs(Unman agedType.ByValA rray, SizeConst=8)]
        > public byte[] strSBCSoftwareV ersion;
        > public Int32 dSBCSoftwareChe cksum;
        > [MarshalAs(Unman agedType.ByValA rray, SizeConst=32)]
        > public byte[] strBarcode;
        > }
        >
        > I have tried the following code, but I get the exception "BinaryFormatte r
        > Version incompatibility ." when it executes.
        >
        > MemoryStream ms = new MemoryStream(by tes); // bytes is a byte[]
        > BinaryFormatter bf = new BinaryFormatter ();
        > m_CompleteIllum StateMsg = (MIX_CompleteIl lumStateMsg)bf. Deserialize(ms) ;[/color]
        //[color=blue]
        > m_CompleteIllum StateMsg is a MESSAGE_LOG_HEA DER_STRUCT2
        >
        >[/color]


        Comment

        Working...