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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,64599bfe530783cd X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-19 21:14:31 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread3.news.pas.earthlink.net.POSTED!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Any examples of Byte Ordering Functions References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Mon, 20 Oct 2003 04:14:30 GMT NNTP-Posting-Host: 63.184.105.104 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.pas.earthlink.net 1066623270 63.184.105.104 (Sun, 19 Oct 2003 21:14:30 PDT) jrcarter010@earthlink.net NNTP-Posting-Date: Sun, 19 Oct 2003 21:14:30 PDT Xref: archiver1.google.com comp.lang.ada:1167 Date: 2003-10-20T04:14:30+00:00 List-Id: tmoran@acm.org wrote: > type source is record > a,b,c,d : bytes; > end record; > for source use record > a at 0 range 0 .. 7; > b at 1 range 0 .. 7; > c at 2 range 0 .. 7; > d at 3 range 0 .. 7; > end record; for Source'Size use 32; is probably a good idea, too. > type target is record > d,c,b,a : bytes; > end record; Even better, type Target is new Source; This creates conversions named Source and Target that reorder your bytes for you automatically. > for target use record > d at 0 range 0 .. 7; > c at 1 range 0 .. 7; > b at 2 range 0 .. 7; > a at 3 range 0 .. 7; > end record; A derived type may have a different representation than its parent. A size clause here would be a good idea, too. > type dword is new interfaces.integer_32; > type swapped_dword is new interfaces.integer_32; > > function split_dword is new ada.unchecked_conversion(dword, source); > function make_swapped is new ada.unchecked_conversion(target, swapped_dword); > > function byte_swap(input : dword) return swapped_dword is > s : constant source := split_dword(input); > begin > return make_swapped(target'(a=>s.d, b=>s.c, c=>s.b, d=>s.a)); > end byte_swap; return Make_Swapped (Target (S) ); -- Jeff Carter "You cheesy lot of second-hand electric donkey-bottom biters." Monty Python & the Holy Grail 14