473,545 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 52438
See BitConverter.Ge tBytes(...) method

=============== ==
Manish Agarwal
Email: ma***********@h otmail.com
"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP09.phx.gbl...
See BitConverter.Ge tBytes(...) method

=============== ==
Manish Agarwal
Email: ma***********@h otmail.com
"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP11.phx.gbl...
But this will not convert a structor object ...
"Manish Agarwal" <ma***********@ hotmail.com> wrote in message
news:uJ******** ******@TK2MSFTN GP09.phx.gbl...
See BitConverter.Ge tBytes(...) method

=============== ==
Manish Agarwal
Email: ma***********@h otmail.com
"Prabhu" <Pr******@e-tapaal.com> wrote in message
news:#6******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP11.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 [] StructureToByte Array(object obj)

{

int len = Marshal.SizeOf( obj);

byte [] arr = new byte[len];

IntPtr ptr = Marshal.AllocHG lobal(len);

Marshal.Structu reToPtr(obj, ptr, true);

Marshal.Copy(pt r, arr, 0, len);

Marshal.FreeHGl obal(ptr);

return arr;

}

static void ByteArrayToStru cture(byte [] bytearray, ref object obj)

{

int len = Marshal.SizeOf( obj);

IntPtr i = Marshal.AllocHG lobal(len);

Marshal.Copy(by tearray,0, i,len);

obj = Marshal.PtrToSt ructure(i, obj.GetType());

Marshal.FreeHGl obal(i);

}

Thanks and Regards

Prakash Prabhu

"William Stacey" <st***********@ mvps.org> wrote in message
news:eB******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP09.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 [] StructureToByte Array(object obj)

{

int len = Marshal.SizeOf( obj);

byte [] arr = new byte[len];

IntPtr ptr = Marshal.AllocHG lobal(len);

Marshal.Structu reToPtr(obj, ptr, true);

Marshal.Copy(pt r, arr, 0, len);

Marshal.FreeHGl obal(ptr);

return arr;

}

static void ByteArrayToStru cture(byte [] bytearray, ref object obj)

{

int len = Marshal.SizeOf( obj);

IntPtr i = Marshal.AllocHG lobal(len);

Marshal.Copy(by tearray,0, i,len);

obj = Marshal.PtrToSt ructure(i, obj.GetType());

Marshal.FreeHGl obal(i);

}

Thanks and Regards

Prakash Prabhu

"William Stacey" <st***********@ mvps.org> wrote in message
news:eB******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP11.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
11780
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. How do I convert between the IntPtr and my managed byte that I want to copy the data into??? -dave
5
18815
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
10153
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 the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array)...
15
34568
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
9319
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 i convert from a byte array to a double array?
2
2620
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 the most similar object is the OleDbDataReader. I have the following method, ¿is it correct what I am doing? By doing oRdr.GetBytes(0, 0, bBytes,...
0
4290
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 = File.Create(sFile); BinaryWriter bw = new BinaryWriter(fs);
5
13701
by: Star | last post by:
Hi all, Let's suppose we have this: byte buffer = new byte; buffer = 0x04; buffer = 0xF1; buffer = 0xB4;
3
11536
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
13283
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 static final long serialVersionUID = 1L;
0
7470
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7428
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7760
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3455
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1887
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.