By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

How to convert a structure to byte array.

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
See BitConverter.GetBytes(...) method

=================
Manish Agarwal
Email: ma***********@hotmail.com
"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6**************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #2

P: n/a
But this will not convert a structor object ...
"Manish Agarwal" <ma***********@hotmail.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
See BitConverter.GetBytes(...) method

=================
Manish Agarwal
Email: ma***********@hotmail.com
"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6**************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #3

P: n/a
You have to implement a method which internally use GetBytes( ) method to
convert each members in bytes

"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#7**************@TK2MSFTNGP11.phx.gbl...
But this will not convert a structor object ...
"Manish Agarwal" <ma***********@hotmail.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
See BitConverter.GetBytes(...) method

=================
Manish Agarwal
Email: ma***********@hotmail.com
"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6**************@TK2MSFTNGP11.phx.gbl...
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



Nov 15 '05 #4

P: n/a
> 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];
}
}
Nov 15 '05 #5

P: n/a
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

"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6**************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #6

P: n/a
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" <st***********@mvps.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
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

"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6**************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #7

P: n/a
:-) 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" <Pr******@e-tapaal.com> wrote in message
news:Op**************@TK2MSFTNGP09.phx.gbl...
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" <st***********@mvps.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
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

"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6**************@TK2MSFTNGP11.phx.gbl...
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



Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.