comp.lang.ada
 help / color / mirror / Atom feed
* AVR Usart send number string with no 'Image
@ 2013-04-25  1:27 Rego, P.
  2013-04-25  2:03 ` Shark8
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Rego, P. @ 2013-04-25  1:27 UTC (permalink / raw)


I am trying to send a numeric string over Usart in an AVR ATmega2560, using avr-elf-windows. So I coded a function Put (Data : String) which works well, and a function Put (Data : Unsigned_8) which actually sends the Character represented by Data : Unsigned_8. 

But I need now a function which sends really the number, not its equivalent character. So I think I could convert the number into a string and send it. The problem is that avr-elf-windows has no 'Image available, so I have to implement it by hand. How could I do it?

Regards



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  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-25  4:54 ` rrr.eee.27
  2013-05-01 20:35 ` Rego, P.
  2 siblings, 1 reply; 20+ messages in thread
From: Shark8 @ 2013-04-25  2:03 UTC (permalink / raw)


    declare
	Subtype Digit is Character Range '0'..'9';
	Function To_String( Input : Interfaces.Unsigned_8 ) Return String is
	    Subtype Digital is Interfaces.Unsigned_8 Range 0..9;
	begin
	    if Input in Digital then
		Return (1 => Digit'Val( Natural(Input) + Character'Pos('0') ));
	    else
		declare
		    Use Type Interfaces.Unsigned_8;
		    D2 : Constant Interfaces.Unsigned_8:= Input / 10;
		    D1 : Constant Interfaces.Unsigned_8:= Input mod 10;
		begin
		    Return To_String(D2) & To_String(D1);
		end;
	    end if;
	end To_String;
    
	Subtype Byte_Range is Integer range 0..255;
	X : Integer := 4;
    begin
	While X in byte_range loop
	    Ada.Text_IO.Put_Line( To_String( Interfaces.Unsigned_8(X) ) );
	    X:= X*2;
	end loop;
    end;



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-25  1:27 AVR Usart send number string with no 'Image Rego, P.
  2013-04-25  2:03 ` Shark8
@ 2013-04-25  4:54 ` rrr.eee.27
  2013-04-25 10:58   ` Rego, P.
  2013-05-01 20:35 ` Rego, P.
  2 siblings, 1 reply; 20+ messages in thread
From: rrr.eee.27 @ 2013-04-25  4:54 UTC (permalink / raw)


On Thursday, April 25, 2013 3:27:52 AM UTC+2, Rego, P. wrote:
> I am trying to send a numeric string over Usart in an AVR ATmega2560, using avr-elf-windows. So I coded a function Put (Data : String) which works well, and a function Put (Data : Unsigned_8) which actually sends the Character represented by Data : Unsigned_8. 
> 
> 
> 
> But I need now a function which sends really the number, not its equivalent character. So I think I could convert the number into a string and send it. The problem is that avr-elf-windows has no 'Image available, so I have to implement it by hand. How could I do it?
> 

See the package AVR.Int_Img in AVR-Ada.  It contains various attempts for generating string representations of integers.

http://sourceforge.net/p/avr-ada/code/ci/master/tree/avr/avr_lib/avr-int_img.ads
http://sourceforge.net/p/avr-ada/code/ci/master/tree/avr/avr_lib/avr-int_img.adb

The next version of AVR-Ada will add a new way based on Dmitry's String_Edit package.

http://www.dmitry-kazakov.de/ada/strings_edit.htm

HTH 
   Rolf



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-25  2:03 ` Shark8
@ 2013-04-25 10:55   ` Rego, P.
  2013-04-26  2:28     ` Rego, P.
  0 siblings, 1 reply; 20+ messages in thread
From: Rego, P. @ 2013-04-25 10:55 UTC (permalink / raw)


On Wednesday, April 24, 2013 11:03:25 PM UTC-3, Shark8 wrote:
> declare
> 	Subtype Digit is Character Range '0'..'9';
> 	Function To_String( Input : Interfaces.Unsigned_8 ) Return String is
> 	    Subtype Digital is Interfaces.Unsigned_8 Range 0..9;
> 	begin
> 	    if Input in Digital then
> 		Return (1 => Digit'Val( Natural(Input) + Character'Pos('0') ));
> 	    else
> 		declare
> 		    Use Type Interfaces.Unsigned_8;
> 		    D2 : Constant Interfaces.Unsigned_8:= Input / 10;
> 		    D1 : Constant Interfaces.Unsigned_8:= Input mod 10;
> 		begin
> 		    Return To_String(D2) & To_String(D1);
> 		end;
> 	    end if;
> 	end To_String;
> 	Subtype Byte_Range is Integer range 0..255;
> 	X : Integer := 4;
>     begin
> 	While X in byte_range loop
> 	    Ada.Text_IO.Put_Line( To_String( Interfaces.Unsigned_8(X) ) );
> 	    X:= X*2;
> 	end loop;
>     end;

Thanks Shark8, much interesting your solution.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-25  4:54 ` rrr.eee.27
@ 2013-04-25 10:58   ` Rego, P.
  0 siblings, 0 replies; 20+ messages in thread
From: Rego, P. @ 2013-04-25 10:58 UTC (permalink / raw)


On Thursday, April 25, 2013 1:54:55 AM UTC-3, rrr.e...@gmail.com wrote:
> See the package AVR.Int_Img in AVR-Ada.  It contains various attempts for generating string representations of integers.
> http://sourceforge.net/p/avr-ada/code/ci/master/tree/avr/avr_lib/avr-int_img.ads
> http://sourceforge.net/p/avr-ada/code/ci/master/tree/avr/avr_lib/avr-int_img.adb

Thank you Rolf. It helps me for sure.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-25 10:55   ` Rego, P.
@ 2013-04-26  2:28     ` Rego, P.
  2013-04-26  5:50       ` Simon Wright
  2013-04-26 14:52       ` Shark8
  0 siblings, 2 replies; 20+ messages in thread
From: Rego, P. @ 2013-04-26  2:28 UTC (permalink / raw)


I tried this, but the compiler pointed that I could not use the second stack, so this recursion is not allowed.

On Thursday, April 25, 2013 7:55:38 AM UTC-3, Rego, P. wrote:
> On Wednesday, April 24, 2013 11:03:25 PM UTC-3, Shark8 wrote:
> 
> > declare
> 
> > 	Subtype Digit is Character Range '0'..'9';
> 
> > 	Function To_String( Input : Interfaces.Unsigned_8 ) Return String is
> 
> > 	    Subtype Digital is Interfaces.Unsigned_8 Range 0..9;
> 
> > 	begin
> 
> > 	    if Input in Digital then
> 
> > 		Return (1 => Digit'Val( Natural(Input) + Character'Pos('0') ));
> 
> > 	    else
> 
> > 		declare
> 
> > 		    Use Type Interfaces.Unsigned_8;
> 
> > 		    D2 : Constant Interfaces.Unsigned_8:= Input / 10;
> 
> > 		    D1 : Constant Interfaces.Unsigned_8:= Input mod 10;
> 
> > 		begin
> 
> > 		    Return To_String(D2) & To_String(D1);
> 
> > 		end;
> 
> > 	    end if;
> 
> > 	end To_String;
> 
> > 	Subtype Byte_Range is Integer range 0..255;
> 
> > 	X : Integer := 4;
> 
> >     begin
> 
> > 	While X in byte_range loop
> 
> > 	    Ada.Text_IO.Put_Line( To_String( Interfaces.Unsigned_8(X) ) );
> 
> > 	    X:= X*2;
> 
> > 	end loop;
> 
> >     end;
> 
> 
> 
> Thanks Shark8, much interesting your solution.




^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  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
  1 sibling, 1 reply; 20+ messages in thread
From: Simon Wright @ 2013-04-26  5:50 UTC (permalink / raw)


"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;



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-26  2:28     ` Rego, P.
  2013-04-26  5:50       ` Simon Wright
@ 2013-04-26 14:52       ` Shark8
  2013-04-26 21:10         ` Shark8
  1 sibling, 1 reply; 20+ messages in thread
From: Shark8 @ 2013-04-26 14:52 UTC (permalink / raw)


On Thursday, April 25, 2013 8:28:49 PM UTC-6, Rego, P. wrote:
> I tried this, but the compiler pointed that I could not use the second stack, so this recursion is not allowed.
> 
> > 
> > Thanks Shark8, much interesting your solution.

Ah, sorry that it didn't work. I forgot about the second stack absenteeism (and thus no recursion).



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-26 14:52       ` Shark8
@ 2013-04-26 21:10         ` Shark8
  2013-04-30  3:00           ` Rego, P.
  0 siblings, 1 reply; 20+ messages in thread
From: Shark8 @ 2013-04-26 21:10 UTC (permalink / raw)


I rewrote it iteratively; I'm pretty sure an extended return doesn't require the second-stack so you should be fine.

    declare
	Subtype String_3 is String(1..3);
	use Interfaces;
	
	-- Because the secondary stack is disabled we cannot use either of
	-- (a) recursion, or (b) unconstrained return-types. Therefore we
	-- need to work both iteratively and with fixed-length strings.
	
	Function To_String( Input : Unsigned_8 ) Return String_3 is
	    -- A temporary variable for manipulation, initialized to input.
	    Working : Unsigned_8 := Input;
	begin
	    -- Extended return, we do not have to initialize any characters
	    -- because they will be in range '0'..'9' with leading zeros.
	    Return Result : String_3 do
		-- We assign the digit it's proper value, based on its
		-- position within the string.
		For Digit in reverse Result'Range loop
		    Result(Digit):= Character'Val(
				      -- We add the mod 10 value of working
				      -- to the value of '0' to get the proper
				      -- digit-character.
				      Natural(Working mod 10) + 
				      Character'Pos('0')
		     		);
		    -- We adjust our working-variable, by dividing it by 10.
		    Working:= Working / 10;
		end loop;
	    end return;
	End To_String;
	  
	-- Testing variable.
	Temp : Natural := 4;
    begin
	-- Test
	while Temp < 256 loop
	    Ada.Text_IO.Put_Line( 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;



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-26  5:50       ` Simon Wright
@ 2013-04-30  2:58         ` Rego, P.
  0 siblings, 0 replies; 20+ messages in thread
From: Rego, P. @ 2013-04-30  2:58 UTC (permalink / raw)


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!



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-26 21:10         ` Shark8
@ 2013-04-30  3:00           ` Rego, P.
  0 siblings, 0 replies; 20+ messages in thread
From: Rego, P. @ 2013-04-30  3:00 UTC (permalink / raw)


On Friday, April 26, 2013 6:10:27 PM UTC-3, Shark8 wrote:
> I rewrote it iteratively; I'm pretty sure an extended return doesn't require the second-stack so you should be fine.
>     declare
> 	Subtype String_3 is String(1..3);
> 	use Interfaces;
> 	-- Because the secondary stack is disabled we cannot use either of
> 	-- (a) recursion, or (b) unconstrained return-types. Therefore we
> 	-- need to work both iteratively and with fixed-length strings.
> 	Function To_String( Input : Unsigned_8 ) Return String_3 is
> 	    -- A temporary variable for manipulation, initialized to input.
> 	    Working : Unsigned_8 := Input;
> 	begin
> 	    -- Extended return, we do not have to initialize any characters
> 	    -- because they will be in range '0'..'9' with leading zeros.
> 	    Return Result : String_3 do
> 		-- We assign the digit it's proper value, based on its
> 		-- position within the string.
> 		For Digit in reverse Result'Range loop
> 		    Result(Digit):= Character'Val(
> 				      -- We add the mod 10 value of working
> 				      -- to the value of '0' to get the proper
> 				      -- digit-character.
> 				      Natural(Working mod 10) + 
> 				      Character'Pos('0')
> 		     		);
> 		    -- We adjust our working-variable, by dividing it by 10.
> 		    Working:= Working / 10;
> 		end loop;
> 	    end return;
> 	End To_String;
> 	-- Testing variable.
> 	Temp : Natural := 4;
>     begin
> 	-- Test
> 	while Temp < 256 loop
> 	    Ada.Text_IO.Put_Line( 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;

Thanks Shark8, it very worked! Also thanks for your comments.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-04-25  1:27 AVR Usart send number string with no 'Image Rego, P.
  2013-04-25  2:03 ` Shark8
  2013-04-25  4:54 ` rrr.eee.27
@ 2013-05-01 20:35 ` Rego, P.
  2013-05-01 21:00   ` Simon Wright
  2013-05-01 22:32   ` Shark8
  2 siblings, 2 replies; 20+ messages in thread
From: Rego, P. @ 2013-05-01 20:35 UTC (permalink / raw)


Now I am trying to implement the inverse (String to Unsigned_8), but something is missing...

I tried

   function String_To_Unsigned_8 (Input : String) return Unsigned_8 is
      Result : Unsigned_8 := 0;
   begin
      for Digit in Input'Range loop
         Result := 10*Result + (Character'Pos (Input (Digit)) - Character'Pos ('0'));
      end loop;
         
      return Result;
   end String_To_Unsigned_8;

But the result is not right. Maybe did I forget something?



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  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:32   ` Shark8
  1 sibling, 1 reply; 20+ messages in thread
From: Simon Wright @ 2013-05-01 21:00 UTC (permalink / raw)


"Rego, P." <pvrego@gmail.com> writes:

> Now I am trying to implement the inverse (String to Unsigned_8), but
> something is missing...
>
> I tried
>
>    function String_To_Unsigned_8 (Input : String) return Unsigned_8 is
>       Result : Unsigned_8 := 0;
>    begin
>       for Digit in Input'Range loop
>          Result := 10*Result + (Character'Pos (Input (Digit)) - Character'Pos ('0'));
>       end loop;
>          
>       return Result;
>    end String_To_Unsigned_8;
>
> But the result is not right. Maybe did I forget something?

Do you have any white space in Input?



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-05-01 21:00   ` Simon Wright
@ 2013-05-01 22:07     ` Rego, P.
  2013-05-01 22:30       ` Jeffrey Carter
  2013-05-01 23:29       ` Dennis Lee Bieber
  0 siblings, 2 replies; 20+ messages in thread
From: Rego, P. @ 2013-05-01 22:07 UTC (permalink / raw)


On Wednesday, May 1, 2013 6:00:41 PM UTC-3, Simon Wright wrote:
> Do you have any white space in Input?

No, I am testing with 

   Put (Data => Unsigned_8_To_String (IMAGE.String_To_Unsigned_8 ("523")));

but my output is 011.

I tested also in String_To_Unsigned_8 just with 
   Result := Character'Pos (Input (Digit)) - Character'Pos ('0'); 

to test if the single output is ok (and it is so). But something should be missing in the 10*Result + ... part.



^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  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
  1 sibling, 1 reply; 20+ messages in thread
From: Jeffrey Carter @ 2013-05-01 22:30 UTC (permalink / raw)


On 05/01/2013 03:07 PM, Rego, P. wrote:
>
> No, I am testing with
>
>     Put (Data => Unsigned_8_To_String (IMAGE.String_To_Unsigned_8 ("523")));

523 isn't a valid value for an Unsigned_8. Since operations on modular types 
wrap, you're getting 523 mod 256 = 11.

-- 
Jeff Carter
"I would never want to belong to any club that
would have someone like me for a member."
Annie Hall
41


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-05-01 20:35 ` Rego, P.
  2013-05-01 21:00   ` Simon Wright
@ 2013-05-01 22:32   ` Shark8
  2013-05-01 23:00     ` Rego, P.
  1 sibling, 1 reply; 20+ messages in thread
From: Shark8 @ 2013-05-01 22:32 UTC (permalink / raw)


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;


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-05-01 22:30       ` Jeffrey Carter
@ 2013-05-01 22:46         ` Rego, P.
  0 siblings, 0 replies; 20+ messages in thread
From: Rego, P. @ 2013-05-01 22:46 UTC (permalink / raw)


> 523 isn't a valid value for an Unsigned_8. Since operations on modular types 
> wrap, you're getting 523 mod 256 = 11.

You are right, Jeff. Below 255 is ok. Thanks.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-05-01 22:32   ` Shark8
@ 2013-05-01 23:00     ` Rego, P.
  2013-05-01 23:09       ` Shark8
  0 siblings, 1 reply; 20+ messages in thread
From: Rego, P. @ 2013-05-01 23:00 UTC (permalink / raw)


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.


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-05-01 23:00     ` Rego, P.
@ 2013-05-01 23:09       ` Shark8
  0 siblings, 0 replies; 20+ messages in thread
From: Shark8 @ 2013-05-01 23:09 UTC (permalink / raw)


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;


^ permalink raw reply	[flat|nested] 20+ messages in thread

* Re: AVR Usart send number string with no 'Image
  2013-05-01 22:07     ` Rego, P.
  2013-05-01 22:30       ` Jeffrey Carter
@ 2013-05-01 23:29       ` Dennis Lee Bieber
  1 sibling, 0 replies; 20+ messages in thread
From: Dennis Lee Bieber @ 2013-05-01 23:29 UTC (permalink / raw)


On Wed, 1 May 2013 15:07:36 -0700 (PDT), "Rego, P." <pvrego@gmail.com>
declaimed the following in comp.lang.ada:

> On Wednesday, May 1, 2013 6:00:41 PM UTC-3, Simon Wright wrote:
> > Do you have any white space in Input?
> 
> No, I am testing with 
> 
>    Put (Data => Unsigned_8_To_String (IMAGE.String_To_Unsigned_8 ("523")));
> 
> but my output is 011.

	Presuming Unsigned_8 IS an 8-bit modular type, the valid range of
data is 0..255.

	523 mod 256 => 11

	Nothing wrong that I can see...
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2013-05-01 23:29 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox