comp.lang.ada
 help / color / mirror / Atom feed
From: Mark Johnson <mark_h_johnson@raytheon.com>
Subject: Re: Hi byte and Lo Byte Question
Date: Wed, 22 Aug 2001 18:11:08 -0500
Date: 2001-08-22T18:11:08-05:00	[thread overview]
Message-ID: <3B843C0C.9A3282B@raytheon.com> (raw)
In-Reply-To: 9m0gn6$b6j$1@zeus.orl.lmco.com

mop wrote:

> I need to parse data into hi byte and lo byte. Does Ada have some built in
> macros or .... I could use?  From the looks of it no.
>

Let's assume you have a two byte unsigned integer. You want to get the two
bytes out of it. At least two ways come to mind...
[1] The "C" way - see below.
[2] An unchecked conversion from a 16 bit integer to a [record or] array of
two bytes.
and there can certainly be others.
As Ted mentioned, you don't mention byte order as being significant. Since
you are porting from C however, I'll assume you want to treat the 16 bit
value as an integer & get the "low byte" to refer to the least significant 8
bits - no matter what machine you run this on.

>
> here's the C version .... Need the Ada version as am new to Ada.
>
> lo = LOBYTE(Value);   -- need Ada version for this LOBYTE macro
> hi = HIBYTE(Value);    -- need Ada version for this HIBYTE macro
> WORD.Data.Word5_Data_Value_In_HB_LB_Order[0] = hi;
> WORD.Data.Word5_Data_Value_In_HB_LB_Order[1] = lo;

I guess I can assume...
#define LOBYTE(Value) (Value&0xff)
#define HIBYTE(Value) (Value >>8)
if Value is unsigned, or if Value might be signed...
#define HIBYTE(Value) ((Value >>8)&0xff)
The "C" way in Ada would be to do the same arithmetic in a function. In Ada
95, modular types work great on this. If you think you have a stupid
optimizer - use pragma Inline to expand it in line. Yes, it is ten lines of
text instead of two, but in this case, GNAT will generate pretty much the
same code as gcc will do for C.

I'll leave the unchecked conversion as an exercise. I don't recommend it due
to the byte order problem. Also, you generally use Ada to use strong typing,
not get away from it.
  --Mark





  parent reply	other threads:[~2001-08-22 23:11 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-22 14:48 Hi byte and Lo Byte Question mop
2001-08-22 15:56 ` Larry Kilgallen
2001-08-22 16:21 ` Ted Dennison
2001-08-22 23:11 ` Mark Johnson [this message]
2001-08-23  0:23   ` Darren New
2001-08-23 14:13     ` Mark Johnson
2001-08-23 17:15       ` Darren New
2001-08-24  0:00       ` Robert Dewar
2001-08-24  7:52         ` Martin Dowie
2001-08-23 14:43   ` Marin David Condic
2001-08-24  8:09     ` Peter Dulimov
2001-08-24 16:20       ` Warren W. Gay VE3WWG
2001-08-24 17:05         ` Marin David Condic
2001-08-24 17:46           ` Warren W. Gay VE3WWG
2001-08-25  7:15             ` Simon Wright
2001-08-27 16:33               ` Warren W. Gay VE3WWG
2001-08-23  1:26 ` DuckE
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox