From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.4 required=5.0 tests=AC_FROM_MANY_DOTS,BAYES_00 autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4e9860765413318c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-08-23 07:57:37 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!psinet-eu-nl!psiuk-p4!uknet!psiuk-n!news.pace.co.uk!nh.pace.co.uk!not-for-mail From: "Marin David Condic" Newsgroups: comp.lang.ada Subject: Re: Hi byte and Lo Byte Question Date: Thu, 23 Aug 2001 10:43:50 -0400 Organization: Posted on a server owned by Pace Micro Technology plc Message-ID: <9m34r7$6ka$1@nh.pace.co.uk> References: <9m0gn6$b6j$1@zeus.orl.lmco.com> <3B843C0C.9A3282B@raytheon.com> NNTP-Posting-Host: dhcp-200-133.miami.pace.co.uk X-Trace: nh.pace.co.uk 998577831 6794 136.170.200.133 (23 Aug 2001 14:43:51 GMT) X-Complaints-To: newsmaster@news.cam.pace.co.uk NNTP-Posting-Date: 23 Aug 2001 14:43:51 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Xref: archiver1.google.com comp.lang.ada:12331 Date: 2001-08-23T14:43:51+00:00 List-Id: If you are working with Modular types, its not too hard. Two examples below compile and specifically work with Interfaces.Unsigned_16. They could be modified or made generic with some effort if a more general solution is desired. with Interfaces ; use Interfaces ; function High_Byte ( Value : in Interfaces.Unsigned_16) return Interfaces.Unsigned_16 is begin return Interfaces.Shift_Right ( Value => (Value and 16#FF00#), Amount => 8) ; end High_Byte ; with Interfaces ; use Interfaces ; function Low_Byte ( Value : in Interfaces.Unsigned_16) return Interfaces.Unsigned_16 is begin return (Value and 16#00FF#) ; end Low_Byte ; Here's a proposition worth discussing: In a lot of embedded apps and/or other apps that need to deal with things like communication streams, it would be very handy to be able to easily translate whatever data you have into raw bytes or turn raw bytes into the data you want. You typically need to do this efficiently and don't want to move the data. You can certainly get there with Unchecked_Conversion, but this can be a pain in the ASCII and might be inefficient - requiring the data to be moved. You can also get there by laboriously creating your own overlays - but this can rewuire a lot of declaring if you have a lot of things you want to treat as raw bytes. What about an attribute of any type that yields an array of Ada.Streams.Stream_Element_Array ? Similarly, going in the other direction: for X in My_Type'Raw_Data (My_Data)'Range loop Some_Storage_Element := My_Type'Raw_Data (My_Data) (X) ; end loop ; or Some_Field := My_Type'Overlay (Some_Stream_Element_Array).Other_Field ; Maybe it should be an attribute of an Object rather than a Type? Don't know what can be done about safety in the event that the sizes don't line up, but I'll leave that to the compiler guys. I'm not fussy about the details (like the names) but it sure would be nice to have a means of very easily, efficiently and quickly treating any object as a stream of bytes - with the caveat being that I've just removed the safety net (as with Unchecked_Conversion) and I can now royally shoot myself in the foot. Comments? MDC -- Marin David Condic Senior Software Engineer Pace Micro Technology Americas www.pacemicro.com Enabling the digital revolution e-Mail: marin.condic@pacemicro.com Web: http://www.mcondic.com/ "Mark Johnson" wrote in message news:3B843C0C.9A3282B@raytheon.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 > >