From: tonyg <tonythegair@googlemail.com>
Subject: Re: Hexadecimal and stream element arrays
Date: Mon, 19 Apr 2010 10:26:58 -0700 (PDT)
Date: 2010-04-19T10:26:58-07:00 [thread overview]
Message-ID: <a0464d60-f93e-4577-93cf-4e8ad853c071@q23g2000yqd.googlegroups.com> (raw)
In-Reply-To: f407e916-5e1d-4c00-b569-9dc9cc2e065d@5g2000yqj.googlegroups.com
On Apr 19, 6:02 pm, tonyg <tonytheg...@googlemail.com> wrote:
> On Apr 19, 5:27 pm, tonyg <tonytheg...@googlemail.com> wrote:
>
>
>
> > 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.
>
> I tried the first way and thought I would prefer the hex.
> I got an error as the compiler is expecting a second value in the
> range.
> Second way is definetly the way forward I think , Thanks again for a
> very detailed reply, its saved me loads of time :)
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 :=
('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
Result (K) := X (integer(Working_Copy) mod integer (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
next prev parent reply other threads:[~2010-04-19 17:26 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 [this message]
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