How to convert a structure to byte array.

P

Prabhu

Hi,

I have to send a structure through TCPClient socket. we can send only
byte array through the socket,

So please any one can help me by telling How to convert a struct object into
an byte array..

Thanks & Regards
Prakash Prabhu
 
M

Manish Agarwal

See BitConverter.GetBytes(...) method

=================
Manish Agarwal
Email: (e-mail address removed)
 
P

Prabhu

But this will not convert a structor object ...


Manish Agarwal said:
See BitConverter.GetBytes(...) method

=================
Manish Agarwal
Email: (e-mail address removed)
 
M

Manish Agarwal

You have to implement a method which internally use GetBytes( ) method to
convert each members in bytes
 
G

Guest

I have to send a structure through TCPClient socket. we can send only
byte array through the socket,

So please any one can help me by telling How to convert a struct object into
an byte array..

I don't have a worked out example but this might give you a tip.
(Try replacing "version" with your structure)
I
unsafe {
fixed (void *p=&version) {
byte *ptrByte = (byte *)p;
iReturn=ptrByte[3];
}
}
 
W

William Stacey

If struct or contains only native types (i.e. int, byte, fixed length array)
you can use the Marshal class and StructToPtr.
 
P

Prabhu

I worked., Thanx a lot...

I have used below functions for "Converting from structure to byte array"
and "byte array to structure"

static byte [] StructureToByteArray(object obj)

{

int len = Marshal.SizeOf(obj);

byte [] arr = new byte[len];

IntPtr ptr = Marshal.AllocHGlobal(len);

Marshal.StructureToPtr(obj, ptr, true);

Marshal.Copy(ptr, arr, 0, len);

Marshal.FreeHGlobal(ptr);

return arr;

}

static void ByteArrayToStructure(byte [] bytearray, ref object obj)

{

int len = Marshal.SizeOf(obj);

IntPtr i = Marshal.AllocHGlobal(len);

Marshal.Copy(bytearray,0, i,len);

obj = Marshal.PtrToStructure(i, obj.GetType());

Marshal.FreeHGlobal(i);

}



Thanks and Regards

Prakash Prabhu
 
W

William Stacey

:) In the second one, you could remove the first copy by "fixing" the
managed array and getting pointer to first byte. Would need to use pointer
and unsafe code.

--
William Stacey, MVP

Prabhu said:
I worked., Thanx a lot...

I have used below functions for "Converting from structure to byte array"
and "byte array to structure"

static byte [] StructureToByteArray(object obj)

{

int len = Marshal.SizeOf(obj);

byte [] arr = new byte[len];

IntPtr ptr = Marshal.AllocHGlobal(len);

Marshal.StructureToPtr(obj, ptr, true);

Marshal.Copy(ptr, arr, 0, len);

Marshal.FreeHGlobal(ptr);

return arr;

}

static void ByteArrayToStructure(byte [] bytearray, ref object obj)

{

int len = Marshal.SizeOf(obj);

IntPtr i = Marshal.AllocHGlobal(len);

Marshal.Copy(bytearray,0, i,len);

obj = Marshal.PtrToStructure(i, obj.GetType());

Marshal.FreeHGlobal(i);

}



Thanks and Regards

Prakash Prabhu

William Stacey said:
If struct or contains only native types (i.e. int, byte, fixed length array)
you can use the Marshal class and StructToPtr.

--
William Stacey, MVP

object
into
 
P

Peter Duniho

prem said:
[...]
what is (ref object obj) arguments which is used but structure to bytearray function is working properly can u explain about 'ref object obj' arguments

I or anyone else here could. But it makes a lot more sense for you to
just read about it in MSDN:
http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

I would consider marshaling and explicitly-aligned structures to be
advanced .NET/C# topics. As a rough rule of thumb, I'd say they are
probably not appropriate for someone still learning language features
like passing-by-reference.

Pete
 
P

Peter Duniho

Prem said:
I have structure as follows:

struct data {int no; string name; int id};

I am converting this structure into bytearray. I need to convert this back into structure format. For that I need to convert first members into integer and string. How to convert bytearray into structure ?

BinaryReader will read the data from a byte[] for you. However, since
you haven't described the format of the data, it's impossible to know
exactly how you'll need to use it. Hopefully the defaults will be fine
(32-bit little-endian integer, and UTF8 string). Even with that, you
need some way of knowing how long the string is. Again, you have failed
to provide enough information for anyone to know that, so you're on your
own there too.

Pete
 

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