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: a07f3367d7,f3514db0a21f9b44 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!q23g2000yqd.googlegroups.com!not-for-mail From: tonyg Newsgroups: comp.lang.ada Subject: Re: Hexadecimal and stream element arrays Date: Mon, 19 Apr 2010 10:26:58 -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 1271698018 3155 127.0.0.1 (19 Apr 2010 17:26:58 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 19 Apr 2010 17:26:58 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: q23g2000yqd.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: g2news1.google.com comp.lang.ada:10067 Date: 2010-04-19T10:26:58-07:00 List-Id: On Apr 19, 6:02=A0pm, tonyg wrote: > On Apr 19, 5:27=A0pm, tonyg wrote: > > > > > 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 con= vert > > > > > to hexadecimal and display on the screen. I was wondering if anyo= ne > > > > > knows of a simple way to do this? > > > > > To make myself more clear I have already dealt with the serial comm= s > > > > 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_El= ement); > > > > =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. > > 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 :=3D ('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 =3D 4 bits Working_Copy : Ada.Streams.Stream_Element :=3D E; use type Ada.Streams.Stream_Element; First_Character : Natural :=3D 0; Base : constant :=3D 16; begin for K in reverse Result'First .. Result'Length loop Result (K) :=3D X (integer(Working_Copy) mod integer (Base) ); Working_Copy :=3D Working_Copy / Base; if Working_Copy =3D 0 then First_Character :=3D 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