montgrimpulo
2015-01-29 17:36:20 UTC
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.
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.