PC Review


Reply
Thread Tools Rate Thread

BIg-Endian vs Lil-Endian

 
 
Chris P
Guest
Posts: n/a
 
      13th Nov 2003
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


 
Reply With Quote
 
 
 
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      13th Nov 2003
"Chris P" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
David Kline [msft]
Guest
Posts: n/a
 
      14th Nov 2003
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.


"Chris P" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Chris P
Guest
Posts: n/a
 
      14th Nov 2003
David,

Thank You.

Chris
"David Kline [msft]" <(E-Mail Removed)> wrote in message
news:gNYsb.144067$275.440806@attbi_s53...
> 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.
>
>
> "Chris P" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: big-endian and little-endian Ignacio Machin ( .NET/ C# MVP ) Microsoft C# .NET 0 5th May 2009 08:57 PM
Re: big-endian and little-endian Jeff Johnson Microsoft C# .NET 0 5th May 2009 06:26 PM
Byte Ordering : Little endian - Big Endian : Binary Files mohamed.alam78@gmail.com Microsoft C# .NET 5 22nd Jul 2006 08:26 PM
Big endian / Little endian question David Lindgren Windows XP Hardware 4 20th Apr 2004 01:29 PM
Big endian / Little endian question David Lindgren Microsoft Windows 2000 Hardware 3 20th Apr 2004 01:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:53 AM.