From: "Rego, P." <pvrego@gmail.com>
Subject: Re: AVR Usart send number string with no 'Image
Date: Mon, 29 Apr 2013 19:58:10 -0700 (PDT)
Date: 2013-04-29T19:58:10-07:00 [thread overview]
Message-ID: <df42f459-ce5d-4a47-965f-863bfa17c466@googlegroups.com> (raw)
In-Reply-To: <ly1u9xu9gn.fsf@pushface.org>
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!
next prev parent reply other threads:[~2013-04-30 2:58 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
2013-04-30 2:58 ` Rego, P. [this message]
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