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 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.36.66 with SMTP id s2mr22771170qad.6.1367665139955; Sat, 04 May 2013 03:58:59 -0700 (PDT) Path: y6ni7345qax.0!nntp.google.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed.news.ucla.edu!nrc-news.nrc.ca!News.Dal.Ca!news.litech.org!news.stack.nl!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: AVR Usart send number string with no 'Image Date: Fri, 26 Apr 2013 06:50:16 +0100 Organization: A noiseless patient Spider Message-ID: References: <931385b2-3520-4d7a-b56f-6d0d0b06d467@googlegroups.com> <508e7264-46c3-4c53-94e6-6bb427fb3908@googlegroups.com> <7bb4c21a-be68-433e-a517-484798a8a687@googlegroups.com> Mime-Version: 1.0 Injection-Info: mx05.eternal-september.org; posting-host="abea7aea1fa87bdeaf110ddb7626f1db"; logging-data="10461"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/KCs8S7Dqg5mOjPOV8luUrqJBNmxho4BQ=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (darwin) Cancel-Lock: sha1:I7zKlO19a39OxMpwNiaR70d8KRc= sha1:8HTfML8/XniDHmfAP20rR2XtEFE= Content-Type: text/plain Date: 2013-04-26T06:50:16+01:00 List-Id: "Rego, P." writes: > I tried this, but the compiler pointed that I could not use the second > stack, so this recursion is not allowed. 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 U8_To_String;