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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f3514db0a21f9b44 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!c1g2000vbc.googlegroups.com!not-for-mail From: tonyg Newsgroups: comp.lang.ada Subject: Re: Hexadecimal and stream element arrays Date: Tue, 20 Apr 2010 02:00:37 -0700 (PDT) Organization: http://groups.google.com Message-ID: <3cc08523-62e9-4fca-8911-a912c72c9465@c1g2000vbc.googlegroups.com> References: <2f4313a5-bb3d-4f7f-8a86-7c8f7d549c53@k41g2000yqf.googlegroups.com> <87eiib2mlg.fsf@ludovic-brenta.org> NNTP-Posting-Host: 92.233.204.67 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1271754037 29202 127.0.0.1 (20 Apr 2010 09:00:37 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 20 Apr 2010 09:00:37 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: c1g2000vbc.googlegroups.com; posting-host=92.233.204.67; posting-account=28F2IwkAAACL1Z5nRC-dE7zuvWdbWC7P User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.1.9) Gecko/20100402 Ubuntu/9.10 (karmic) Firefox/3.5.9,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:10086 Date: 2010-04-20T02:00:37-07:00 List-Id: On Apr 19, 9:50=A0pm, Ludovic Brenta wrote: > tonyg writes on comp.lang.ada: > > > Changed the hex function to > > function To_Hex (E : in Ada.Streams.Stream_Element) return String > > is > > =A0 =A0-- Warning: not compiled and not tested... > > =A0 =A0X : constant array (0 .. 15) of Character :=3D > > (1) I should have written: > > =A0 =A0 X : constant array (Ada.Streams.Stream_Element range 0 .. 15) of = Character :=3D > > > =A0 =A0 =A0('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', > > =A0 =A0 =A0 'C', 'D', 'E', 'F'); > > =A0 =A0Result : String (1 .. Ada.Streams.Stream_Element'Size / 4); -- 1= hex digits =3D 4 bits > > =A0 =A0Working_Copy : Ada.Streams.Stream_Element :=3D E; > > =A0 =A0use type Ada.Streams.Stream_Element; > > =A0 =A0First_Character : Natural :=3D 0; > > =A0 =A0Base : constant =A0:=3D 16; > > begin > > =A0 =A0for K in reverse Result'First .. Result'Length loop > > (2) and this should be: > > =A0 =A0 =A0for K in reverse Result'Range loop > > > =A0 =A0 =A0 Result (K) :=3D X (integer(Working_Copy) mod integer (Base)= ); > > (3) and thanks to (1), this can come back to the simpler: > > =A0 =A0 =A0 =A0Result (K) :=3D X (Working_Copy mod Base); > > > =A0 =A0 =A0 Working_Copy :=3D Working_Copy / Base; > > =A0 =A0 =A0 if Working_Copy =3D 0 then > > =A0 =A0 =A0 =A0 =A0First_Character :=3D K; > > =A0 =A0 =A0 =A0 =A0exit; > > =A0 =A0 =A0 end if; > > =A0 =A0end loop; > > =A0 =A0return Result (First_Character .. Result'Last); > > end To_Hex; > > > It still seems to be dropping a few zeros though and I'm stumped where > > its going wrong > > I tested it with 42 and 0 and correctly got 2A and 0, so I don't know > what you mean by that. > > Maybe the fact that the result has a variable width is a problem? =A0If > so, here is a fixed-width variant which is actually a bit simpler: > > function To_Hex (E : in Ada.Streams.Stream_Element) return String is > =A0 =A0X : constant array (Ada.Streams.Stream_Element range 0 .. 15) > =A0 =A0 =A0of Character :=3D > =A0 =A0 =A0('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', '= C', > =A0 =A0 =A0 'D', 'E', 'F'); > =A0 =A0Result : String > =A0 =A0 =A0(1 .. Ada.Streams.Stream_Element'Size / 4) -- 1 hex digits =3D= 4 bits > =A0 =A0 =A0:=3D (others =3D> '0'); > =A0 =A0Working_Copy : Ada.Streams.Stream_Element :=3D E; > =A0 =A0use type Ada.Streams.Stream_Element; > =A0 =A0Base : constant :=3D 16; > begin > =A0 =A0for K in reverse Result'Range loop > =A0 =A0 =A0 Result (K) :=3D X (Working_Copy mod Base); > =A0 =A0 =A0 Working_Copy :=3D Working_Copy / Base; > =A0 =A0end loop; > =A0 =A0return Result; > end To_Hex; > > and this yields 2A and 00 for my "test vector". > > -- > Ludovic Brent Its the variable width I think.