comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Any examples of Byte Ordering Functions
Date: Mon, 20 Oct 2003 04:14:30 GMT
Date: 2003-10-20T04:14:30+00:00	[thread overview]
Message-ID: <GiJkb.3306$np1.1706@newsread3.news.pas.earthlink.net> (raw)
In-Reply-To: <qBEkb.825218$Ho3.226586@sccrnsc03>

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




  reply	other threads:[~2003-10-20  4:14 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-15 17:42 Any examples of Byte Ordering Functions Bill Sheehan
2003-10-15 18:55 ` Larry Kilgallen
2003-10-15 18:44   ` Hyman Rosen
2003-10-16  1:01     ` James Rogers
2003-10-16 13:32       ` Hyman Rosen
2003-10-16 14:59       ` Martin Dowie
2003-10-16 20:00         ` tmoran
2003-10-17 12:08           ` Martin Dowie
2003-10-19 15:22         ` chris
2003-10-19 17:25           ` (see below)
2003-10-19 13:45     ` chris
2003-10-19 16:53       ` Martin Dowie
2003-10-19 17:52         ` chris
2003-10-19 19:24           ` Martin Dowie
2003-10-19 20:47           ` Jeffrey Carter
2003-10-19 22:53             ` tmoran
2003-10-20  4:14               ` Jeffrey Carter [this message]
2003-10-23 20:36       ` Laurent Pautet
2003-10-23 21:37         ` Simon Wright
2003-10-24  4:37           ` Simon Wright
2003-10-26 15:05           ` Georg Bauhaus
2003-10-23 20:44       ` Laurent Pautet
2003-10-23 21:03       ` Laurent Pautet
2003-10-15 19:58 ` Frank J. Lhota
2003-10-16 16:28 ` Stephen Leake
replies disabled

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