IPAddress class in System.Net namespace also has functions for swapping the
byte orders of short, int, and long.
int netOrder = IPAddress.HostToNetworkOrder(val1);
int hostOrder = IPAddress.HostToNetworkOrder(val2);
For any other type you have to revervse the Byte[] yourself as Marcin
indicated. You want to be carefull that the machine uses the expected byte
ordering that you are converting to. This is handled for you in IPAddress
class.
For instance if you reverse the array, be sure to check the byte order of
the machine first. You may not need to reverse it - "network byte order" is
Big Endian.
Here's a sample of converting a double to and from network order:
private static byte[] HostToNetworkOrder(double d)
{
byte[] data = BitConverter.GetBytes(d);
if (BitConverter.IsLittleEndian)
{
Array.Reverse(data);
}
return data;
}
private static double NetworkToHostOrder(byte[] data)
{
if (BitConverter.IsLittleEndian)
{
Array.Reverse(data);
}
return BitConverter.ToDouble(data, 0);
}
HTH,
Eric Cadwell
http://www.origincontrols.com