byte array to struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swts
    New Member
    • Feb 2008
    • 16

    byte array to struct

    this is in reference to one of the posts i found in the .NET forum
    i need to send structure across thru the socket....


    static byte [] StructureToByte Array(object obj)
    >
    > {
    >
    > int len = Marshal.SizeOf( obj);
    >
    > byte [] arr = new byte[len];
    >
    > IntPtr ptr = Marshal.AllocHG lobal(len);
    >
    > Marshal.Structu reToPtr(obj, ptr, true);
    >
    > Marshal.Copy(pt r, arr, 0, len);
    >
    > Marshal.FreeHGl obal(ptr);
    >
    > return arr;
    >
    > }
    >
    > static void ByteArrayToStru cture(byte [] bytearray, ref object obj)
    >
    > {
    >
    > int len = Marshal.SizeOf( obj);
    >
    > IntPtr i = Marshal.AllocHG lobal(len);
    >
    > Marshal.Copy(by tearray,0, i,len);
    >
    > obj = Marshal.PtrToSt ructure(i, obj.GetType());
    >
    > Marshal.FreeHGl obal(i);
    >
    > }
    >
    >i was able to convert the struct to byte array but to perform the vice versa wat argument shud be sent in ref object??
    is there any other method or modification i could do?
    thanks in advance
  • jjvainav
    New Member
    • Feb 2008
    • 25

    #2
    That object is an object of type you want to cast your byte array to. There is another way so you don't need that object.

    Code:
    static object ByteArrayToStructure(byte[] bytearray)
    {
      IntPtr ptr = IntPtr.Zero;
    		
      try
      {
        ptr = Marshal.AllocHGlobal(bytearray.Length);
        Marshal.Copy(bytearray, 0, ptr, bytearray.Length);
    				
        return Marshal.PtrToStructure(ptr, typeof(SomeType));
      }
      finally
      {
        if(ptr != IntPtr.Zero)
        {
          Marshal.FreeHGlobal(ptr);
        }
      }
    }
    Where the SomeType in typeof(SomeType ) is the type of object you want to convert your byte array to.


    Originally posted by swts
    this is in reference to one of the posts i found in the .NET forum
    i need to send structure across thru the socket....


    static byte [] StructureToByte Array(object obj)
    >
    > {
    >
    > int len = Marshal.SizeOf( obj);
    >
    > byte [] arr = new byte[len];
    >
    > IntPtr ptr = Marshal.AllocHG lobal(len);
    >
    > Marshal.Structu reToPtr(obj, ptr, true);
    >
    > Marshal.Copy(pt r, arr, 0, len);
    >
    > Marshal.FreeHGl obal(ptr);
    >
    > return arr;
    >
    > }
    >
    > static void ByteArrayToStru cture(byte [] bytearray, ref object obj)
    >
    > {
    >
    > int len = Marshal.SizeOf( obj);
    >
    > IntPtr i = Marshal.AllocHG lobal(len);
    >
    > Marshal.Copy(by tearray,0, i,len);
    >
    > obj = Marshal.PtrToSt ructure(i, obj.GetType());
    >
    > Marshal.FreeHGl obal(i);
    >
    > }
    >
    >i was able to convert the struct to byte array but to perform the vice versa wat argument shud be sent in ref object??
    is there any other method or modification i could do?
    thanks in advance

    Comment

    Working...