comp.lang.ada
 help / color / mirror / Atom feed
* Integer to String
@ 2015-01-29 17:36 montgrimpulo
  2015-01-29 17:51 ` Simon Wright
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: montgrimpulo @ 2015-01-29 17:36 UTC (permalink / raw)


Hi,

I want to solve in Ada:

from Integers (1,2,3,4,5,6) to String "123456" and then to Integer 123456,
to avoid multiplications and additions.

I tried with some Attributes and solved the second part.

with Ada.Text_IO; use Ada.Text_IO;
procedure ItoS is
   package I_IO is new Integer_IO (Integer);
   use I_IO;
   num : integer ; char : character ; str : String(1..6);
begin
   num := 7;
   char := character'Val(num);
   New_Line;Put("num = ");Put(num);
   New_Line;Put("char= ");Put(char);
   char:='7';
   num:=character'Pos(char);
   New_Line(2);Put("char= ");Put(char);
   New_Line;Put("num= ");Put(num);
   str:="123456";
   num:=Integer'Value(str);
   New_Line(2);Put("num= ");Put(num);
(20)   str:=Integer'Image(num);
   New_Line;Put("str= ");Put(str);
end ItoS;

However, the outcome is :
num =           7
char=

char= 7
num=          55

num=      123456

raised CONSTRAINT_ERROR : itos.adb:20 length check failed.



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

* Re: Integer to String
  2015-01-29 17:36 Integer to String montgrimpulo
@ 2015-01-29 17:51 ` Simon Wright
  2015-01-29 19:16 ` Jeffrey Carter
  2015-01-29 20:39 ` Anh Vo
  2 siblings, 0 replies; 4+ messages in thread
From: Simon Wright @ 2015-01-29 17:51 UTC (permalink / raw)


montgrimpulo <aghte@hotlinemail.com> writes:

>    num : integer ; char : character ; str : String(1..6);
> begin

>    str:="123456";
>    num:=Integer'Value(str);
>    New_Line(2);Put("num= ");Put(num);
> (20)   str:=Integer'Image(num);

> raised CONSTRAINT_ERROR : itos.adb:20 length check failed.

Integer'Image prefixes the digits of the result with a '-' if the number
is negative and a space if it's positive.

You could just write

   Put (Integer'Image (num));

or

   num := Integer'Value ("123456");
   Put_Line ("str=" & Integer'Image (num));

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

* Re: Integer to String
  2015-01-29 17:36 Integer to String montgrimpulo
  2015-01-29 17:51 ` Simon Wright
@ 2015-01-29 19:16 ` Jeffrey Carter
  2015-01-29 20:39 ` Anh Vo
  2 siblings, 0 replies; 4+ messages in thread
From: Jeffrey Carter @ 2015-01-29 19:16 UTC (permalink / raw)


On 01/29/2015 10:36 AM, montgrimpulo wrote:
> 
> from Integers (1,2,3,4,5,6) to String "123456" and then to Integer 123456,
> to avoid multiplications and additions.
> 
> I tried with some Attributes and solved the second part.

For the first part (converting a list of Integers in 0 .. 9 to a String), you
could take the 'Image of each integer and store the digit Character in the image
into the correct position in a String.

-- 
Jeff Carter
"Ditto, you provincial putz?"
Blazing Saddles
86


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

* Re: Integer to String
  2015-01-29 17:36 Integer to String montgrimpulo
  2015-01-29 17:51 ` Simon Wright
  2015-01-29 19:16 ` Jeffrey Carter
@ 2015-01-29 20:39 ` Anh Vo
  2 siblings, 0 replies; 4+ messages in thread
From: Anh Vo @ 2015-01-29 20:39 UTC (permalink / raw)


On Thursday, January 29, 2015 at 9:36:21 AM UTC-8, montgrimpulo wrote:
> -- ...
>    New_Line(2);Put("num= ");Put(num);
> (20)   str:=Integer'Image(num);
>    New_Line;Put("str= ");Put(str);
> end ItoS;

(20) should not be here. Otherwise, syntax violation is resulted. It must be cut and paste error.

> However, the outcome is :
> num =           7
> char=

The 7th character (BEL) is a controlled character. Thus, nothing is printed. 

> num=      123456
> 
> raised CONSTRAINT_ERROR : itos.adb:20 length check failed.

Others have already explained this exception.

Anh Vo



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

end of thread, other threads:[~2015-01-29 20:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-29 17:36 Integer to String montgrimpulo
2015-01-29 17:51 ` Simon Wright
2015-01-29 19:16 ` Jeffrey Carter
2015-01-29 20:39 ` Anh Vo

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