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
Email: manish...@hotmail.com
"Prabhu" <Prab...@e-tapaal.com> wrote in message
news:#6P0ieY5...@TK2MSFTNGP11.phx.gbl...
"Manish Agarwal" <manish...@hotmail.com> wrote in message
news:uJuTpnY5...@TK2MSFTNGP09.phx.gbl...
"Prabhu" <Prab...@e-tapaal.com> wrote in message
news:#7jhAtZ5...@TK2MSFTNGP11.phx.gbl...
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, MVP
"Prabhu" <Prab...@e-tapaal.com> wrote in message
news:#6P0ieY5...@TK2MSFTNGP11.phx.gbl...
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, MVP
"Prabhu" <Prab...@e-tapaal.com> wrote in message
news:OpotWll5...@TK2MSFTNGP09.phx.gbl...
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/
{
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
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
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
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#