By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,627 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,627 IT Pros & Developers. It's quick & easy.

Converting to a byte array for a socket.send

P: n/a
Hi,

I'm trying (or struggling) to convert some C code to C#.

The existing C client is sending a structure via a TCP socket to a network
server. The structure contains a mix of int and char data types [with NO
data alignment i.e. pack(1)].

I can re-create the structure in c# with LayoutKind.Explicit, but how do I
convert this structure to a byte array for use with the Socket.Send()
method?
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The solution I know of consists of creating a byte array of size of your
structure and using BitConverter class to obtain values of integers and
chars from the struct in the order they are defined. Maybe there's another
possible solution I don't know of, but this one works.

Example:
struct Foo
{
int number;
char c;

public byte[] ToBytes()
{
byte[] numberBytes = BitConverter.GetBytes(number);
byte[] charBytes = BitConverter.GetBytes(c);
byte[] result = new byte[numberBytes.Length + charBytes.Length];
//use Array.Copy to copy contents of both arrays to the result, I
don't know the exact signature off my head
return result;
}
}

"Shreddy" <sh*****@nospam.protean-systems.com> wrote in message
news:TP********************@pipex.net...
Hi,

I'm trying (or struggling) to convert some C code to C#.

The existing C client is sending a structure via a TCP socket to a network
server. The structure contains a mix of int and char data types [with NO
data alignment i.e. pack(1)].

I can re-create the structure in c# with LayoutKind.Explicit, but how do I convert this structure to a byte array for use with the Socket.Send()
method?

Nov 17 '05 #2

P: n/a
Hi,

A simple solution would be adding a method like :

public byte[] GetBytes()
{
byte[] buff = new byte[ XXX ] ; //you know the size apriori.
Array.Copy( BitConverter.GetBytes( aInt), buff, 0, 0 , 4 ); // not sure
if this is the correct set of parameters !!!
Array.Copy( BitConverter.GetBytes( aInt), buff, 0, 4 , 4 ); //similar

return buff;
}
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Shreddy" <sh*****@nospam.protean-systems.com> wrote in message
news:TP********************@pipex.net...
Hi,

I'm trying (or struggling) to convert some C code to C#.

The existing C client is sending a structure via a TCP socket to a network
server. The structure contains a mix of int and char data types [with NO
data alignment i.e. pack(1)].

I can re-create the structure in c# with LayoutKind.Explicit, but how do
I convert this structure to a byte array for use with the Socket.Send()
method?

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.