comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: AVR Usart send number string with no 'Image
Date: Wed, 1 May 2013 15:32:42 -0700 (PDT)
Date: 2013-05-01T15:32:42-07:00	[thread overview]
Message-ID: <0b7431ad-fa72-45d4-ac26-20b2c0785d16@googlegroups.com> (raw)
In-Reply-To: <443a92d3-2327-423d-ad74-174b0a69e4c4@googlegroups.com>

On Wednesday, May 1, 2013 2:35:02 PM UTC-6, Rego, P. wrote:
> Now I am trying to implement the inverse (String to Unsigned_8),
> But the result is not right. Maybe did I forget something?

    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;


  parent reply	other threads:[~2013-05-01 22:32 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 [this message]
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