comp.lang.ada
 help / color / mirror / Atom feed
From: "Rego, P." <pvrego@gmail.com>
Subject: Re: AVR Usart send number string with no 'Image
Date: Wed, 1 May 2013 16:00:19 -0700 (PDT)
Date: 2013-05-01T16:00:19-07:00	[thread overview]
Message-ID: <36095985-768d-42f0-a6ab-17eb0e896a4d@googlegroups.com> (raw)
In-Reply-To: <0b7431ad-fa72-45d4-ac26-20b2c0785d16@googlegroups.com>

On Wednesday, May 1, 2013 7:32:42 PM UTC-3, Shark8 wrote:
>     declare
> 	Use Interfaces;
> 	Subtype Digit is Character Range '0'..'9';
> 	Subtype String3 is String(1..3);
> 
> 	Function From_String(Input : String3) return Unsigned_8 is
> 	    Working	: String3	:= Input;
> 	    Index	: Unsigned_8	:= 1;
> 	begin
> 	    -- preprocessing string: spaces are treated as 0.
> 	    for C of Working loop
> 		C := (if C = ' ' then '0' else C);
> 		-- throw error if invalid characters exist.
> 		if C not in Digit then
> 		    raise Numeric_Error;
> 		end if;
> 		-- Note a case statement might habe been
> 		-- nore appropriate for this block.
> 	    end loop;
> 
> 	    Return Result : Unsigned_8 := 0 do
> 		for C of reverse Working loop
> 		    -- We add the current character's value, multiplied
> 		    -- by its position, to modify result.
> 		    Result:= Result +
> 			Index * (Character'Pos(C) - Character'Pos('0'));
> 		    -- The following works because wrap-around isn't an error.
> 		    -- If we weren't using Unsigned_8 we would need a cast.
> 		    Index:= Index * 10;
> 		end loop;
> 	    End return;
> 	End From_String;

Thanks Shark8, even better than my last one.


  reply	other threads:[~2013-05-01 23:00 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.
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. [this message]
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