From: tonyg on
On Apr 19, 5:27 pm, tonyg <tonytheg...(a)googlemail.com> wrote:
> On Apr 19, 5:24 pm, Ludovic Brenta <ludo...(a)ludovic-brenta.org> wrote:
>
>
>
> > tonyg wrote on comp.lang.ada:
>
> > > On Apr 19, 4:44 pm, tonyg <tonytheg...(a)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 :)
From: John B. Matthews on
In article
<f7e8f2a4-a989-4b74-b66e-b248d684d5ae(a)z11g2000yqz.googlegroups.com>,
tonyg <tonythegair(a)googlemail.com> wrote:

> On Apr 19, 4:44 pm, tonyg <tonytheg...(a)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

Here's one approach that's convenient for command line use:

<http://sites.google.com/site/drjohnbmatthews/hexdump>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
From: tonyg on
On Apr 19, 6:02 pm, tonyg <tonytheg...(a)googlemail.com> wrote:
> On Apr 19, 5:27 pm, tonyg <tonytheg...(a)googlemail.com> wrote:
>
>
>
> > On Apr 19, 5:24 pm, Ludovic Brenta <ludo...(a)ludovic-brenta.org> wrote:
>
> > > tonyg wrote on comp.lang.ada:
>
> > > > On Apr 19, 4:44 pm, tonyg <tonytheg...(a)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
From: Dmitry A. Kazakov on
On Mon, 19 Apr 2010 08:58:16 -0700 (PDT), tonyg wrote:

> On Apr 19, 4:44�pm, tonyg <tonytheg...(a)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

The following is based on:

http://www.dmitry-kazakov.de/ada/strings_edit.htm

with Ada.Streams; use Ada.Streams;
with Strings_Edit; use Strings_Edit;
with Strings_Edit.Integers; use Strings_Edit.Integers;

function Image (Data : Stream_Element_Array) return String is
begin
if Data'Length = 0 then
return "";
end if;
declare
Text : String (1..Data'Length * 3);
Pointer : Integer := 1;
begin
for I in Data'Range loop
Put
( Text, Pointer, Stream_Element'Pos (Data (I)),
Field=>2, Fill=>'0', Justify=>Right, Base=>16
);
Put (Text, Pointer, ' ');
end loop;
return Text (1..Text'Last - 1);
end;
end Image;

An individual item is output

Put
( Text, Pointer, Stream_Element'Pos (Item),
Field=>2, Fill=>'0', Justify=>Right, Base=>16
);

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: tonyg on
On Apr 19, 6:27 pm, "Dmitry A. Kazakov" <mail...(a)dmitry-kazakov.de>
wrote:
> On Mon, 19 Apr 2010 08:58:16 -0700 (PDT), tonyg wrote:
> > On Apr 19, 4:44 pm, tonyg <tonytheg...(a)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
>
> The following is based on:
>
>    http://www.dmitry-kazakov.de/ada/strings_edit.htm
>
> with Ada.Streams;            use Ada.Streams;
> with Strings_Edit;           use Strings_Edit;
> with Strings_Edit.Integers;  use Strings_Edit.Integers;
>
> function Image (Data : Stream_Element_Array) return String is
> begin
>    if Data'Length = 0 then
>       return "";
>    end if;
>    declare
>       Text    : String (1..Data'Length * 3);
>       Pointer : Integer := 1;
>    begin
>       for I in Data'Range loop
>          Put
>          (  Text, Pointer, Stream_Element'Pos (Data (I)),
>             Field=>2, Fill=>'0', Justify=>Right, Base=>16
>          );
>          Put (Text, Pointer, ' ');
>       end loop;
>       return Text (1..Text'Last - 1);
>    end;
> end Image;
>
> An individual item is output
>
>    Put
>    (  Text, Pointer, Stream_Element'Pos (Item),
>        Field=>2, Fill=>'0', Justify=>Right, Base=>16
>    );
>
> --
> Regards,
> Dmitry A. Kazakovhttp://www.dmitry-kazakov.de

Thanks Guys, I forgot how helpful these forums were and how many
people out there used Ada, you reminded me, and I got some working
code \o/