BIg-Endian vs Lil-Endian

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
 
A

Alex Feinman [MVP]

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
 
D

David Kline [msft]

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.
 
C

Chris P

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top