comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <ggsub@pragmada.x10hosting.com>
Subject: Re: Hexadecimal and stream element arrays
Date: Mon, 19 Apr 2010 16:34:32 -0700 (PDT)
Date: 2010-04-19T16:34:32-07:00	[thread overview]
Message-ID: <cb208f1e-ba46-4386-bc53-c85ea5d7e4a5@q31g2000prf.googlegroups.com> (raw)
In-Reply-To: 87aasz2mhx.fsf@ludovic-brenta.org

On Apr 19, 1:52 pm, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
>
> I find it funny you should say that because I thought exactly the
> opposite: letting Ada.Text_IO (which is notoriously slow) do the
> conversion to string and then doing string manipulation is less clear
> and more error-prone, IMHO (a proper implementation would use a simple,
> but full-fledged, finite state machine).  But your approach is certainly
> valid and correct nevertheless.

Funny you should say that ...

Considering that I can write

function Hex_Image (Number : Ada.Streams.Stream_Element) return String
is
   Result : String (1 .. 10);
   Start  : Natural;

   package Stream_Element_IO is new Ada.Text_IO.Modular_IO (Num =>
Ada.Streams.Stream_Element);
begin -- Hex_Image
   Stream_Element_IO.Put (To => Result, Item => Number, Base => 16);
   Start := Ada.Strings.Fixed.Index (Result, "#") + 1;

   return Result (Start .. Result'Last - 1);
end Hex_Image;

and get it right 1st time, compared to the issues you encountered
duplicating (some of) the functionality of Put, seems to me evidence
that this approach is less error prone. I certainly find it simpler
and clearer, too.

Getting this to return a zero-filled String of the correct number of
digits is slightly more complex:

Num_Chars : constant := Ada.Streams.Stream_Element'Size / 4;

Result : String (1 .. Num_Chars + 10);

...

return String'(1 .. Num_Chars - (Result'Last - Start) => '0') & Result
(Start .. Result'Last - 1);



  reply	other threads:[~2010-04-19 23:34 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
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 [this message]
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