From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Hexadecimal and stream element arrays
Date: Mon, 19 Apr 2010 22:50:35 +0200
Date: 2010-04-19T22:50:35+02:00 [thread overview]
Message-ID: <87eiib2mlg.fsf@ludovic-brenta.org> (raw)
In-Reply-To: a0464d60-f93e-4577-93cf-4e8ad853c071@q23g2000yqd.googlegroups.com
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.
next prev parent reply other threads:[~2010-04-19 20:50 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-19 15:44 Hexadecimal and stream element arrays tonyg
2010-04-19 15:58 ` tonyg
2010-04-19 16:24 ` Ludovic Brenta
2010-04-19 16:27 ` tonyg
2010-04-19 17:02 ` tonyg
2010-04-19 17:26 ` tonyg
2010-04-19 20:50 ` Ludovic Brenta [this message]
2010-04-20 9:00 ` tonyg
2010-04-20 9:25 ` Peter Hermann
2010-04-19 18:05 ` Warren
2010-04-19 19:21 ` Jeffrey R. Carter
2010-04-19 19:28 ` Warren
2010-04-19 23:21 ` John B. Matthews
2010-04-19 23:22 ` Adam Beneschan
2010-04-20 14:04 ` Warren
2010-04-20 14:46 ` J-P. Rosen
2010-04-20 15:52 ` Warren
2010-04-19 19:17 ` Jeffrey R. Carter
2010-04-19 20:52 ` Ludovic Brenta
2010-04-19 23:34 ` Jeffrey R. Carter
2010-04-19 17:20 ` John B. Matthews
2010-04-19 17:27 ` Dmitry A. Kazakov
2010-04-19 17:55 ` tonyg
2010-04-20 7:25 ` Stephen Leake
2010-04-20 22:21 ` Jeffrey R. Carter
2010-04-21 12:38 ` 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