473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to convert a structure to byte array.

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
7 52375
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
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
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
> 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
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
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
:-) 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave | last post by:
We are writing some code to interface to a DLL. The DLL has a callback method that returns the address of a byte buffer and the buffer length. I have defined the byte pointer as an IntPtr. ...
5
by: kevinniu | last post by:
Hi everyone, In c#, what is the fastest way(include unsafe) to convert a array of bytes(which really contains the bytes of a double array) to a arry of double. thanks
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
2
by: antonio matos | last post by:
hi people. I have to acess a database, and there are some fields that are binary. when i acess that field is read like an array of bytes. but in reality it's an array of doubles!!! how can...
2
by: Jaime Stuardo | last post by:
Hi all... I'm trying to retrieve a SQLXML query using VB.NET. When I programmed in VB 6.0, I used Stream object to accomplish this which was trivial. I cannot do the same thing in VB.NET. Here...
0
by: Jim Rasmussen | last post by:
I need some help in converting a byte array to pdf. Could someone give me an example of how in C#? Here is what I have so far, using System.IO; string sFile = "c:\testpdf.pdf"; FileStream fs =...
5
by: Star | last post by:
Hi all, Let's suppose we have this: byte buffer = new byte; buffer = 0x04; buffer = 0xF1; buffer = 0xB4;
3
by: dpsairam | last post by:
Hi, How to convert a byte array to image....i mean i have to convert a Byte array into image format.....please hellp.........i need the java code... Thanking You.
1
by: phpuser123 | last post by:
I want to convert my object to byte array and then next to an object and run a method..The codes are below: public class test_serialisation implements Serializable{ /** * */ private...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.