BIg-Endian vs Lil-Endian

  • Thread starter Thread starter Chris P
  • Start date Start date
C

Chris P

I am reading bytes from a serial port which come from a device which uses
the Big-Endian storage format. My question is, can I use the
BitConverter.ToDouble(bytearray, position) method to convert those bytes to
a double? In other words, will the system take into account that the byte
order needs to be swapped?

Conversely, I need to write longs and double to the serial device in
Big-Endian format. How would I do that?

Thanks,

Chris
 
Chris P said:
I am reading bytes from a serial port which come from a device which uses
the Big-Endian storage format. My question is, can I use the
BitConverter.ToDouble(bytearray, position) method to convert those bytes to
a double? In other words, will the system take into account that the byte
order needs to be swapped?

Conversely, I need to write longs and double to the serial device in
Big-Endian format. How would I do that?

BitConverter will not be able to swap bytes for you. You will need to do it
yourself and then use BitConverter (or, if you are writing to the port, get
bytes using BitConverter and then swap the byte order). You can P/Invoke
htonl and ntohl but that is no help with doubles
 
Chris,

You should be able to use the BitConverter to convert your doubles to longs
through byte arrays (ex: long l =
BitConverter.ToInt64(BitConverter.GetBytes(double)); ) and then convert them
between host and network order using IPAddress.HostToNetworkOrder() and
IPAddress.NetworkToHostOrder().

Hope this helps,
David Kline
Microsoft .NET Compact Framework

This posting is provided "AS IS" with no warranties, and confers no rights.
 
David,

Thank You.

Chris
David Kline said:
Chris,

You should be able to use the BitConverter to convert your doubles to longs
through byte arrays (ex: long l =
BitConverter.ToInt64(BitConverter.GetBytes(double)); ) and then convert them
between host and network order using IPAddress.HostToNetworkOrder() and
IPAddress.NetworkToHostOrder().

Hope this helps,
David Kline
Microsoft .NET Compact Framework

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top