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-Thread: 103376,f3514db0a21f9b44 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!feeder.news-service.com!newsfeed.straub-nv.de!feeder.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Hexadecimal and stream element arrays Date: Mon, 19 Apr 2010 22:50:35 +0200 Organization: A noiseless patient Spider Message-ID: <87eiib2mlg.fsf@ludovic-brenta.org> References: <2f4313a5-bb3d-4f7f-8a86-7c8f7d549c53@k41g2000yqf.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Mon, 19 Apr 2010 20:50:35 +0000 (UTC) Injection-Info: feeder.eternal-september.org; posting-host="W8c2PZ3nqsYZZWY5/Jpl9A"; logging-data="22150"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OdcMjAqeQ183X9kSjruA6" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:miFvXo5fapYqaaTpSgmIFTkZrX0= sha1:TkYKoI6BXwqLFMh/L5TyYpM3gWw= Xref: g2news2.google.com comp.lang.ada:11049 Date: 2010-04-19T22:50:35+02:00 List-Id: tonyg writes on comp.lang.ada: > Changed the hex function to > function To_Hex (E : in Ada.Streams.Stream_Element) return String > is > -- Warning: not compiled and not tested... > X : constant array (0 .. 15) of Character := (1) I should have written: X : constant array (Ada.Streams.Stream_Element range 0 .. 15) of Character := > ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', > 'C', 'D', 'E', 'F'); > Result : String (1 .. Ada.Streams.Stream_Element'Size / 4); -- 1 hex digits = 4 bits > Working_Copy : Ada.Streams.Stream_Element := E; > use type Ada.Streams.Stream_Element; > First_Character : Natural := 0; > Base : constant := 16; > begin > for K in reverse Result'First .. Result'Length loop (2) and this should be: for K in reverse Result'Range loop > Result (K) := X (integer(Working_Copy) mod integer (Base) ); (3) and thanks to (1), this can come back to the simpler: Result (K) := X (Working_Copy mod Base); > Working_Copy := Working_Copy / Base; > if Working_Copy = 0 then > First_Character := K; > exit; > end if; > end loop; > return 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? If 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 X : constant array (Ada.Streams.Stream_Element range 0 .. 15) of Character := ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); Result : String (1 .. Ada.Streams.Stream_Element'Size / 4) -- 1 hex digits = 4 bits := (others => '0'); Working_Copy : Ada.Streams.Stream_Element := E; use type Ada.Streams.Stream_Element; Base : constant := 16; begin for K in reverse Result'Range loop Result (K) := X (Working_Copy mod Base); Working_Copy := Working_Copy / Base; end loop; return Result; end To_Hex; and this yields 2A and 00 for my "test vector". -- Ludovic Brenta.