6.8. Type Conversion - JulTob/Ada GitHub Wiki

To convert between numeric types we need to do explicit conversion:


procedure strong_typing_example is
   I : Integer;
   F : Float;
begin
   I := 1;   F := 1.0;  --ok
 --I := 1.0; F := 1;    --Not ok
 --I := F;   F := I;    -- Not ok
   I := Integer(F);
   F := Float(I);       --Ok!
end strong_typing_example;
----------------------------------------
type X_t is new Integer;
type Y_t is new Integer;

procedure strong_typing_example is
   X : X_t;
   Y : Y_t;
begin
   X := 1;   Y := 1;  --ok
   X := X_t(Y);
   Y := Y_t(X);       --Ok!
end strong_typing_example;


Definitions must be static. They don’t take variables.

Number to text

There’s an attribute that converts number types to strings:


Number’Image(var);

String_To_Scalar_Demonstration.adb

-- String_To_Scalar_Demonstration.adb
-- This program demonstrates several methods to convert strings to scalar values.
-- Scalar types include integers, floating-point numbers, enumerated types, and modular types.

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO;   use Ada.Float_Text_IO;

procedure String_To_Scalar_Demonstration is
   -- Define an enumerated type for colors in the visible spectrum.
   type Spectrum is 
      (Red, Orange, Yellow, Green, Blue, Indigo, Violet); 
   
   -- Define a modular type with values from 0 to 255.
   type Unsigned is mod 2**8; 
   
   -- Variables for demonstration.
   Num   : Integer  := 0;
   Fnum  : Float    := 0.0;
   Color : Spectrum := Blue;
   Mnum  : Unsigned := 0;

   -- Strings that will be converted to the above scalar types.
   Text_Integer  : constant String := "451";
   Text_Float    : constant String := "360.0";
   Text_Color    : constant String := "Orange";
   Text_Unsigned : constant String := "42";

   -- Variables to store the position of the last character read from the string.
   Integer_Last  : Natural;
   Float_Last    : Natural;
   Spectrum_Last : Natural;
   Modular_Last  : Natural;

   -- Instantiating IO packages for different types.
   package Spectrum_IO is new Text_IO.Enumeration_IO(Enum => Spectrum);
   package Modular_IO  is new Text_IO.Modular_IO(Num => Unsigned);
   package Integer_IO  is new Text_IO.Integer_IO(Num => Integer);
   package Float_IO    is new Text_IO.Float_IO(Num => Float);

begin
   Put_Line("The String Values are:");
   Put("Orange for Enumerated Type, ");
   Put("451 for Integer Type, ");
   Put("360.0 for Float Type, ");
   Put_Line("42 for Unsigned Type.");
   New_Line;

   -- Example 1: Convert using 'Value' attribute
   Put_Line(">>>> Example 1: Using 'Value' Attribute <<<<");
   Color := Spectrum'Value(Text_Color);
   Num   := Integer'Value(Text_Integer);
   Fnum  := Float'Value(Text_Float);
   Mnum  := Unsigned'Value(Text_Unsigned);

   Spectrum_IO.Put(Color); New_Line;
   Integer_IO.Put(Num);    New_Line;
   Float_IO.Put(Fnum);     New_Line;
   Modular_IO.Put(Mnum);   New_Line;
   New_Line;

   -- Example 2: Using pre-instantiated packages
   Put_Line(">>>> Example 2: Using pre-instantiated packages <<<<");
   Integer_Text_IO.Get(From => Text_Integer, Item => Num, Last => Integer_Last);
   Float_Text_IO.Get(From => Text_Float, Item => Fnum, Last => Float_Last);
   Integer_Text_IO.Put(Num); New_Line;
   Float_Text_IO.Put(Fnum, Fore => 3, Aft => 3, Exp => 0);
   New_Line(2);

   -- Example 3: Using custom instantiated packages
   Put_Line(">>>> Example 3: Using own instantiations <<<<");
   Spectrum_IO.Get(From => Text_Color, Item => Color, Last => Spectrum_Last);
   Modular_IO.Get(From => Text_Unsigned, Item => Mnum, Last => Modular_Last);
   Integer_IO.Get(From => Text_Integer, Item => Num, Last => Integer_Last);
   Float_IO.Get(From => Text_Float, Item => Fnum, Last => Float_Last);

   -- Output the results
   Spectrum_IO.Put(Item => Color);  New_Line;
   Integer_IO.Put(Item => Num);     New_Line;
   Float_IO.Put(Item => Fnum, Fore => 3, Aft => 3, Exp => 0); New_Line;
   Modular_IO.Put(Item => Mnum);
   New_Line(2);
   Put_Line(Text & " End of String_To_Scalar_Demonstration " & Text);
end String_To_Scalar_Demonstration;