From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f3514db0a21f9b44 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!g23g2000yqn.googlegroups.com!not-for-mail From: tonyg Newsgroups: comp.lang.ada Subject: Re: Hexadecimal and stream element arrays Date: Mon, 19 Apr 2010 09:27:13 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <2f4313a5-bb3d-4f7f-8a86-7c8f7d549c53@k41g2000yqf.googlegroups.com> NNTP-Posting-Host: 92.233.204.67 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1271694434 8905 127.0.0.1 (19 Apr 2010 16:27:14 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 19 Apr 2010 16:27:14 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: g23g2000yqn.googlegroups.com; posting-host=92.233.204.67; posting-account=28F2IwkAAACL1Z5nRC-dE7zuvWdbWC7P User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.1.9) Gecko/20100402 Ubuntu/9.10 (karmic) Firefox/3.5.9,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:11035 Date: 2010-04-19T09:27:13-07:00 List-Id: On Apr 19, 5:24=A0pm, Ludovic Brenta wrote: > tonyg wrote on comp.lang.ada: > > > On Apr 19, 4:44=A0pm, tonyg 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#. > > =A0 =A0package Stream_Element_IO is > =A0 =A0 =A0new Ada.Text_IO.Modular_IO (Num =3D> Ada.Streams.Stream_Elemen= t); > > =A0 =A0E : Ada.Streams.Stream_Element :=3D ... > begin > =A0 =A0Stream_Element_IO.Put (Item =3D> E, Base =3D> 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 > =A0 =A0-- Warning: not compiled and not tested... > =A0 =A0X : constant array (0 .. 15) of Character :=3D > =A0 =A0 =A0('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', '= C', > 'D', 'E', 'F'); > =A0 =A0Result : String (1 .. Ada.Streams.Stream_Element'Size / 4); -- 1 > hex digits =3D 4 bits > =A0 =A0Working_Copy : Ada.Streams.Stream_Element :=3D E; > =A0 =A0use type Ada.Streams.Stream_Element; > =A0 =A0First_Character : Natural :=3D 0; > =A0 =A0Base : constant :=3D 16; > begin > =A0 =A0for K in reverse Result'Length loop > =A0 =A0 =A0 Result (K) :=3D X (Working_Copy mod Base); > =A0 =A0 =A0 Working_Copy :=3D Working_Copy / Base; > =A0 =A0 =A0 if Working_Copy =3D 0 then > =A0 =A0 =A0 =A0 =A0First_Character :=3D K; > =A0 =A0 =A0 =A0 =A0exit; > =A0 =A0 =A0 end if; > =A0 =A0end loop; > =A0 =A0return Result (First_Character .. Result'Last); > end To_Hex; > > Hope this helps. > > -- > Ludovic Brenta. Thanks Ludovic I'll try that.