IPAddress class in System.Net namespace also has functions for swapping the
byte orders of short, int, and long.
int netOrder = IPAddress.HostT oNetworkOrder(v al1);
int hostOrder = IPAddress.HostT oNetworkOrder(v al2);
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[] HostToNetworkOr der(double d)
{
byte[] data = BitConverter.Ge tBytes(d);
if (BitConverter.I sLittleEndian)
{
Array.Reverse(d ata);
}
return data;
}
private static double NetworkToHostOr der(byte[] data)
{
if (BitConverter.I sLittleEndian)
{
Array.Reverse(d ata);
}
return BitConverter.To Double(data, 0);
}
HTH,
Eric Cadwell
http://www.origincontrols.com
"Govind" <go******@inter worldgroup.com> wrote in message
news:OP******** ******@TK2MSFTN GP12.phx.gbl...
Hi All,
I want to Convert 32 bit integers to byte in right alighed format .
For 32 = the usual way is BitConverter.Ge tBytes(int32)== > xx xx 00 00 ,
but i want right aligned like 00 00 xx xx.Is there any way.
Regards,
Govind.