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 16:09:05 -0700 (PDT)
Date: 2013-05-01T16:09:05-07:00	[thread overview]
Message-ID: <fd4660fe-ba49-47b2-bed0-6b455eea875b@googlegroups.com> (raw)
In-Reply-To: <36095985-768d-42f0-a6ab-17eb0e896a4d@googlegroups.com>

On Wednesday, May 1, 2013 5:00:19 PM UTC-6, Rego, P. wrote:
Now with validation, that is considering not every three numeric-character input is valid:

    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
	    subtype Constrained is Natural Range
		      Natural(Unsigned_8'First)..Natural(Unsigned_8'Last);
	    Working	: String3	:= Input;
	    Index	: Natural	:= 1;
	    Temp	: Constrained	:= 0;
	    has_space	: Boolean	:= False;
	begin
	    -- Preprocessing string: leading spaces are treated as 0,
	    -- non-leading spaces indicate an error.
	    for C of reverse Working loop
		case C is
		when Digit	=> if has_space then Raise Numeric_Error; end if;
		when ' '	=> C:= '0'; has_space:= true;
		when others	=> Raise Numeric_Error;
		end case;
	    end loop;
	    
	    Return Result : Unsigned_8 do
		for C of reverse Working loop
		    -- We add the current character's value, multiplied
		    -- by its position, to modify temp.
		    Temp:= Temp +
			Index * (Character'Pos(C) - Character'Pos('0'));
		    -- Multiply the index by 10.
		    Index:= Index * 10;
		end loop;
		
		-- Finally we copy our temporary into Result.
		Result:= Unsigned_8(Temp);
		
	   exception
		when Constraint_Error => raise Numeric_Error;
	    End return;
	End From_String;


	-- The pad function adds the proper amount of padding.
	Function Pad( S : String; Length : Positive:= 3 ) Return String is
	    ( if S'Length < Length then ' ' & S else S );
	Function Pad( I : Natural ) Return String3 is
	    Working : String renames Pad(I'Img);
	begin
	    -- if the length is 3, then the 'in' test works,
	    if Working in String3 then
		return Working;
	    else
		-- Othereise, we only want the last three characters...
		return working( Working'Last-2..Working'Last );
	    end if;
	end Pad;
	
        -- Testing variable.
        Temp : Natural := 4;
    begin
        -- Test
        while Temp < 256 loop
	    Ada.Text_IO.Put_Line( From_String(Pad(Temp))'Img );
			  --To_String( Unsigned_8(Temp) ) );
            Temp:= Temp *2;
            -- I want to end w/ 255, not 128.
            Temp:= (if temp = 256 then 255 else temp);
        end loop; 
    end;


      reply	other threads:[~2013-05-01 23:09 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.
2013-05-01 23:09       ` Shark8 [this message]
replies disabled

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