473,484 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Byte Array to structure with conversion

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;
}
Nov 16 '05 #1
5 2344
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/de...us/cpref/html/
frlrfsystemruntimeserializationformattersbinarybin aryformatterclassserialize
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."

Nov 16 '05 #2
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
Nov 16 '05 #3
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."

Nov 16 '05 #4
Kevin,

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

Regards,
John
Nov 16 '05 #5
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."

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2083
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
3
14473
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
5
21593
by: Joe Thompson | last post by:
Hi I am new to C# and am rewritting some C++ code. I want to send a byte array over a serial port. The elements of the byte array are really a structure I have populated. My question is, how do...
1
1450
by: ORC | last post by:
Hi, I have posted almost the same message in the compact framework group, but it seems to got lost in a deep message structure :-) so I'll try again: I'll receive a byte array from a serial...
9
12660
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
14
2484
by: Ronodev.Sen | last post by:
i have a C# program that is sending data in a byte array through a socket. the VC++ application server receives data in teh following format.... typedef struct advice { header sHdr; char ...
12
3298
by: O.B. | last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it, "as" is the keyword to use. Unfortunately, the compiler is trying to do the cast a compilation time. See below. /*...
10
6340
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
7176
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
7079
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6809
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4838
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3044
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1355
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
587
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.