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,87b54dbabc658fa2 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.224.75.137 with SMTP id y9mr20668009qaj.3.1367290690458; Mon, 29 Apr 2013 19:58:10 -0700 (PDT) X-Received: by 10.49.35.129 with SMTP id h1mr168937qej.28.1367290690385; Mon, 29 Apr 2013 19:58:10 -0700 (PDT) Path: ef9ni35280qab.0!nntp.google.com!s14no998974qam.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 29 Apr 2013 19:58:10 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=189.110.14.106; posting-account=TRgI1QoAAABSsYi-ox3Pi6N-JEKKU0cu NNTP-Posting-Host: 189.110.14.106 References: <931385b2-3520-4d7a-b56f-6d0d0b06d467@googlegroups.com> <508e7264-46c3-4c53-94e6-6bb427fb3908@googlegroups.com> <7bb4c21a-be68-433e-a517-484798a8a687@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: AVR Usart send number string with no 'Image From: "Rego, P." Injection-Date: Tue, 30 Apr 2013 02:58:10 +0000 Content-Type: text/plain; charset=ISO-8859-1 Date: 2013-04-29T19:58:10-07:00 List-Id: On Friday, April 26, 2013 2:50:16 AM UTC-3, Simon Wright wrote: > Iteration is your friend. > I thought I might find an answer via google, SO or Rosetta Code, but > no. So try this: > > with Ada.Text_IO; use Ada.Text_IO; > with Interfaces; > procedure U8_To_String is > -- If we can't use the secondary stack, we can't write functions > -- that return unconstrained arrays. We know there can never be > -- more than 3 decimal digits in the output. > > subtype Output_String is String (1 .. 3); > function To_String (U : Interfaces.Unsigned_8) return Output_String is > Result : Output_String := (others => ' '); > use type Interfaces.Unsigned_8; > Digit : Interfaces.Unsigned_8 := U mod 10; > Rest_Of_Number : Interfaces.Unsigned_8 := U / 10; > > begin > for J in reverse Result'Range loop > if Digit /= 0 > or else Rest_Of_Number /= 0 > or else J = Result'Last then > Result (J) > := Character'Val (Character'Pos ('0') + Integer (Digit)); > end if; > Digit := Rest_Of_Number mod 10; > Rest_Of_Number := Rest_Of_Number / 10; > -- could exit when Rest_Of_Number = 0 > end loop; > return Result; > end To_String; > begin > Put_Line (To_String (0)); > Put_Line (To_String (90)); > Put_Line (To_String (99)); > Put_Line (To_String (100)); > Put_Line (To_String (101)); > Put_Line (To_String (142)); > Put_Line (To_String (200)); > Put_Line (To_String (201)); > Put_Line (To_String (242)); > Put_Line (To_String (255)); > end Thanks Simon, it worked!