473,856 Members | 1,560 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 52520
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
11826
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
18842
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
10250
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) "Usuario Sin Atribuciones Para Esta Función"
15
34628
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
9401
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
2704
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, 1000) I'm limiting only to 1000 bytes. How can I know the length of the stream?
0
4381
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
13714
by: Star | last post by:
Hi all, Let's suppose we have this: byte buffer = new byte; buffer = 0x04; buffer = 0xF1; buffer = 0xB4;
3
11662
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
13456
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
9765
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10795
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10390
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7934
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7098
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5962
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4177
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.