comp.lang.ada
 help / color / mirror / Atom feed
From: tonyg <tonythegair@googlemail.com>
Subject: Re: Hexadecimal and stream element arrays
Date: Mon, 19 Apr 2010 09:27:13 -0700 (PDT)
Date: 2010-04-19T09:27:13-07:00	[thread overview]
Message-ID: <bb09f117-a8fe-4bb6-a03d-56d63a605c1b@g23g2000yqn.googlegroups.com> (raw)
In-Reply-To: 2f4313a5-bb3d-4f7f-8a86-7c8f7d549c53@k41g2000yqf.googlegroups.com

On Apr 19, 5:24 pm, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
> tonyg wrote on comp.lang.ada:
>
> > On Apr 19, 4:44 pm, tonyg <tonytheg...@googlemail.com> wrote:
>
> > > I have some data coming in from a serial port which I want to convert
> > > to hexadecimal and display on the screen. I was wondering if anyone
> > > knows of a simple way to do this?
>
> > To make myself more clear I have already dealt with the serial comms
> > bit, I am looking to display the stream I have already captured
>
> You could use the predefined library to get a result using the Ada
> "syntax for based literal" (ARM A.10.8(14)), e.g. 16#CE#.
>
>    package Stream_Element_IO is
>      new Ada.Text_IO.Modular_IO (Num => Ada.Streams.Stream_Element);
>
>    E : Ada.Streams.Stream_Element := ...
> begin
>    Stream_Element_IO.Put (Item => E, Base => 16);
>
> (There are variants of Put that send the output to a File_Type or to a
> string).
>
> If you want to avoid the 16## part, you can easily convert the
> stream_elements to a string representation yourself like so:
>
> 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 :=
>      ('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'Length loop
>       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;
>
> Hope this helps.
>
> --
> Ludovic Brenta.

Thanks Ludovic I'll try that.



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