Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert a structure to byte array.

8 views
Skip to first unread message

Prabhu

unread,
Jan 28, 2004, 5:18:36 AM1/28/04
to
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


Manish Agarwal

unread,
Jan 28, 2004, 5:33:25 AM1/28/04
to
See BitConverter.GetBytes(...) method

=================
Manish Agarwal
Email: manish...@hotmail.com


"Prabhu" <Prab...@e-tapaal.com> wrote in message
news:#6P0ieY5...@TK2MSFTNGP11.phx.gbl...

Prabhu

unread,
Jan 28, 2004, 7:39:14 AM1/28/04
to
But this will not convert a structor object ...


"Manish Agarwal" <manish...@hotmail.com> wrote in message
news:uJuTpnY5...@TK2MSFTNGP09.phx.gbl...

Manish Agarwal

unread,
Jan 28, 2004, 7:43:48 AM1/28/04
to
You have to implement a method which internally use GetBytes( ) method to
convert each members in bytes

"Prabhu" <Prab...@e-tapaal.com> wrote in message

news:#7jhAtZ5...@TK2MSFTNGP11.phx.gbl...

Olaf.B...@skyscan.be

unread,
Jan 28, 2004, 8:52:19 AM1/28/04
to
> 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];
}
}


William Stacey

unread,
Jan 28, 2004, 10:55:21 AM1/28/04
to
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" <Prab...@e-tapaal.com> wrote in message
news:#6P0ieY5...@TK2MSFTNGP11.phx.gbl...

Prabhu

unread,
Jan 29, 2004, 6:19:31 AM1/29/04
to
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" <stacey...@mvps.org> wrote in message
news:eBSSqcb5...@TK2MSFTNGP12.phx.gbl...

William Stacey

unread,
Jan 29, 2004, 9:09:07 AM1/29/04
to
:-) 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" <Prab...@e-tapaal.com> wrote in message

news:OpotWll5...@TK2MSFTNGP09.phx.gbl...

David

unread,
Jul 2, 2009, 4:01:23 PM7/2/09
to
See this posting for an easy way to do this. If you have simple structs with just ints, etc, in it, this should work fine...

http://dooba.net/2009/07/c-and-serializing-byte-arrays/

From http://www.developmentnow.com/g/36_2004_1_0_0_205962/How-to-convert-a-structure-to-byte-array-.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

prem

unread,
Oct 30, 2009, 3:05:17 AM10/30/09
to
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);

}


when i try to use the above ByteArrayToStructure function i am facing some errors.


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

Peter Duniho

unread,
Oct 30, 2009, 4:02:34 AM10/30/09
to
prem wrote:
> [...]

> 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

Prem

unread,
Oct 31, 2009, 3:32:58 AM10/31/09
to

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 ?

Prem

unread,
Oct 31, 2009, 3:37:29 AM10/31/09
to

Peter Duniho

unread,
Oct 31, 2009, 1:22:22 PM10/31/09
to
Prem wrote:
> 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

prem

unread,
Nov 7, 2009, 5:52:09 AM11/7/09
to
Hi,

I have a Byte array in which , different structures are placed back to back
in contiguously.

Now I need to split this byte array into different structures.
Right Now I am using "memcpy" for parsing. But I would like to have custom function
for converting this byte array into desired structure.

My idea is split the byte array into different byte arrays according to the size of the structures.
Then converting these byte arrays into corresponding structures using using some custom function .

I need to do this C#

0 new messages