Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > c# > october 2009 >

c# : How to convert a structure to byte array.


William Stacey
1/28/2004 10:55:21 AM
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

[quoted text, click to view]

Olaf.Baeyens NO[at]SPAM skyscan.be
1/28/2004 2:52:19 PM
[quoted text, click to view]

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];
}
}

Prabhu
1/28/2004 3:48:36 PM
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
1/28/2004 4:03:25 PM
See BitConverter.GetBytes(...) method

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


[quoted text, click to view]

Prabhu
1/28/2004 6:09:14 PM
But this will not convert a structor object ...


[quoted text, click to view]

Manish Agarwal
1/28/2004 6:13:48 PM
You have to implement a method which internally use GetBytes( ) method to
convert each members in bytes

[quoted text, click to view]

William Stacey
1/29/2004 9:09:07 AM
:-) 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

[quoted text, click to view]

Prabhu
1/29/2004 4:49:31 PM
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

[quoted text, click to view]

David
7/2/2009 1:01:29 PM
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
prem
10/30/2009 12:05:18 AM
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




Posted via DevelopmentNow.com Groups
Prem
10/31/2009 12:32:58 AM


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 ?


Posted via DevelopmentNow.com Groups
Prem
10/31/2009 12:37:30 AM


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 ?


Posted via DevelopmentNow.com Groups
prem
11/7/2009 2:52:10 AM
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#

Posted via DevelopmentNow.com Groups
AddThis Social Bookmark Button