Convert struct to byte[]
-
Hello all, I create one struct now i have to send this data to socket method
SentTo
. ButSendTo
method require byte[]. Now how can i convert struct type to byte[]. Thanx in advance.
Divyang Mithaiwala System Engineer & Software Developer
-
Hello all, I create one struct now i have to send this data to socket method
SentTo
. ButSendTo
method require byte[]. Now how can i convert struct type to byte[]. Thanx in advance.
Divyang Mithaiwala System Engineer & Software Developer
You will have to write a custom method that does it. We may help you achieving this, if you post the code for your struct. Also, do you have control of both communication endpoints? -------- "I say no to drugs, but they don't listen." - Marilyn Manson
-
You will have to write a custom method that does it. We may help you achieving this, if you post the code for your struct. Also, do you have control of both communication endpoints? -------- "I say no to drugs, but they don't listen." - Marilyn Manson
Hello Michel, Check your mail. or click here[^]. Thanx for responcing.
Divyang Mithaiwala System Engineer & Software Developer
-
Hello Michel, Check your mail. or click here[^]. Thanx for responcing.
Divyang Mithaiwala System Engineer & Software Developer
Huh? -------- "I say no to drugs, but they don't listen." - Marilyn Manson
-
Huh? -------- "I say no to drugs, but they don't listen." - Marilyn Manson
I send you the whole code on your email address. Plz collect it from there.
Divyang Mithaiwala System Engineer & Software Developer
-
Hello all, I create one struct now i have to send this data to socket method
SentTo
. ButSendTo
method require byte[]. Now how can i convert struct type to byte[]. Thanx in advance.
Divyang Mithaiwala System Engineer & Software Developer
Here's how.
public struct MyStruct
{
public int x;
public int y;
public float z;
}IntPtr p;
MyStruct s;
int size;
byte[] buffer;s = new MyStruct();
s.x = 1;
s.y = 2;
s.z = 3.0f;size = sizeof( typeof( MyStruct ) );
p = Marshal.AllocHGlobal( size );
Marshal.PtrToStructure( s, p, true );
buffer = new byte[ size ];Marshal.Copy( p, buffer, 0, size );
Marshal.FreeHGlobal( p );
I hope this helps. Deus caritas est
-
Here's how.
public struct MyStruct
{
public int x;
public int y;
public float z;
}IntPtr p;
MyStruct s;
int size;
byte[] buffer;s = new MyStruct();
s.x = 1;
s.y = 2;
s.z = 3.0f;size = sizeof( typeof( MyStruct ) );
p = Marshal.AllocHGlobal( size );
Marshal.PtrToStructure( s, p, true );
buffer = new byte[ size ];Marshal.Copy( p, buffer, 0, size );
Marshal.FreeHGlobal( p );
I hope this helps. Deus caritas est
Sorry, the
Marshal.PtrToStructure
should beMarshal.StructureToPtr
. Deus caritas est -
Sorry, the
Marshal.PtrToStructure
should beMarshal.StructureToPtr
. Deus caritas estGreate. Good. I will help me throught out my project. Thanx Andy.
Divyang Mithaiwala System Engineer & Software Developer
-
Hello all, I create one struct now i have to send this data to socket method
SentTo
. ButSendTo
method require byte[]. Now how can i convert struct type to byte[]. Thanx in advance.
Divyang Mithaiwala System Engineer & Software Developer
I deal with packets to byte streams quite frequently in client/server applications. Packets are essentially data structures with an assortment of binary and ascii or whatever formatted data. Here are a couple functions you could use, and polymorhisize as you need:
public int Encode( int i32Value, ref byte[] pdu, int off ) { byte[] byte32Int = new byte[ cBytesPerWord32 ]; int netValue = System.Net.IPAddress.HostToNetworkOrder( i32Value ); byte32Int = BitConverter.GetBytes( netValue ); Array.Copy( byte32Int, 0, pdu, off, cBytesPerWord32 ); return cBytesPerWord32; } public int Decode( ref byte[] pdu, int off, ref uint u32Value ) { uint netValue = BitConverter.ToUInt32( pdu, off ); u32Value = (uint)System.Net.IPAddress.NetworkToHostOrder( (int) netValue ); return cBytesPerWord32; }
Although the function above only deals with 32-bit uint's you can imagine how easy it is to add support for 16-bit and 64-bit integers/unsigned integers. Strings are variable length in most cases otherwise a simple Array.Copy() would suffice. So if you have to pass strings, at a minimum you'll be best served with a length byte and quite possible some sort of a type or tag byte - especialy if you have lots of string elements. That way you can handle strings in any order. Good luck Mike Luster CTI/IVR/Telephony SME