comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: AVR Usart send number string with no 'Image
Date: Fri, 26 Apr 2013 06:50:16 +0100
Date: 2013-04-26T06:50:16+01:00	[thread overview]
Message-ID: <ly1u9xu9gn.fsf@pushface.org> (raw)
In-Reply-To: 7bb4c21a-be68-433e-a517-484798a8a687@googlegroups.com

"Rego, P." <pvrego@gmail.com> 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;



  reply	other threads:[~2013-04-26  5:50 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-25  1:27 AVR Usart send number string with no 'Image Rego, P.
2013-04-25  2:03 ` Shark8
2013-04-25 10:55   ` Rego, P.
2013-04-26  2:28     ` Rego, P.
2013-04-26  5:50       ` Simon Wright [this message]
2013-04-30  2:58         ` Rego, P.
2013-04-26 14:52       ` Shark8
2013-04-26 21:10         ` Shark8
2013-04-30  3:00           ` Rego, P.
2013-04-25  4:54 ` rrr.eee.27
2013-04-25 10:58   ` Rego, P.
2013-05-01 20:35 ` Rego, P.
2013-05-01 21:00   ` Simon Wright
2013-05-01 22:07     ` Rego, P.
2013-05-01 22:30       ` Jeffrey Carter
2013-05-01 22:46         ` Rego, P.
2013-05-01 23:29       ` Dennis Lee Bieber
2013-05-01 22:32   ` Shark8
2013-05-01 23:00     ` Rego, P.
2013-05-01 23:09       ` Shark8
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox