Byte Array to structure with conversion

J

John J. Hughes II

I need to convert a byte array to a structure with conversion on the byte
order. I found the below deserilizer which works for getting the data
converted to a structure but it's in the wrong byte order. Basically it's
the whole big/little endian problem.

Is there someway to do this directly or do I need to use the bitconverter
step the array myself?

Regards,
John

public static object RawDeserializeEx( byte[] rawdatas, Type anytype )
{
int rawsize = Marshal.SizeOf( anytype );
if( rawsize > rawdatas.Length )
return null;
GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pinned );
IntPtr buffer = handle.AddrOfPinnedObject();
object retobj = Marshal.PtrToStructure( buffer, anytype );
handle.Free();
return retobj;
}
 
K

Kevin Yu [MSFT]

Hi John,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know the best way to
convert the byte array to an object. If there is any misunderstanding,
please feel free to let me know.

Thanks for sharing your code here. I think the code is correct. Whether the
byte array can be deserialized correctly depends on how it was serialized.
When marshalling an object to a byte array, I think we can try to use
serialization in .NET. We can use a BinaryFormatter to do serialization and
deserialization. Here is an example:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemruntimeserializationformattersbinarybinaryformatterclassserialize
topic.asp

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
J

John J. Hughes II

Kevin,

Sort of no... in the concept of all things are objects yes. In the concept
of the object has been serialized and is self defining no.

I have a C++ program on non-Intel process that is sending my application
data via a serial port. Basically a memory dump. The memory contains
structures. I need to convert the serial data back to a structure for use
by C#.NET.

Example structure (simplified):
[StructLayout(LayoutKind.Sequential, Pack=4)]
struct tst
{
UInt16 f1;
UInt32 f2;
}

Example Data stream:
{0x00,0x20,0x00,0x00,0x00,0x50}

The code from my first message deserialzes it but the endian order is
incorrect.

Should end up with:
tst.f1 = 0x0020
tst.f2 = 0x00000050

This would work but the endian order is incorrect:
tst.f1 = System.BitConverter.ToUInt16(data, 0);
tst.f2 = System.BitConverter.ToUInt32(data, 2);

So I end up do this:
tst.f1 = data[0]<<8 + data[1];
tst.f2 = data[2]<<24 + data[3]<<16 + data[4]<<8 + data[5]

Which can be tedious on long structures and a maintenance problem.

So is there a better way?

Regards,
John
 
K

Kevin Yu [MSFT]

Hi John,

I think what you suppose is correct. It is the big/little endian problem.
Intel CPUs swaps the high 8bit and low 8bit when storing data in memory. As
far as I know, there is no direct convertion in .NET. I think you have to
write your own code to swap them back.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
J

John J. Hughes II

Kevin,

Thanks for the response... Just wanted to make sure before I reinvented the
wheel yet again.

Regards,
John
 
K

Kevin Yu [MSFT]

You're welcome, John.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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