The simplest way to deal with string representations of any scalar type is with the attributes 'Image and 'Value. These attributes act as functions that return the string representation of a scalar object or return the scalar value of a string representation respectively. An example is below:
X : Integer ;
...
X := Integer'Value ("1234") ;
Ada.Text_IO.Put_Line (
Item => "The image of X is: " & Integer'Image (X)) ;
Note that the attribute is associated with the data type. For a Float, you would use Float'Image and Float'Value. Likewise for Boolean, you have Boolean'Image and Boolean'Value. These attributes work for all scalar types, regardless of if they are predefined or user defined types.
Alternatively, the generic packages for scalar text input and output provide the ability to perform I/O either to files or strings with a little bit better control of the representation and format. They are somewhat more complicated, but provide more power to the user. Packages you should look at are: Ada.Text_IO.Integer_IO, Ada.Text_IO.Modular_IO, Ada.Text_IO.Float_IO, Ada.Text_IO.Enumeration_IO where you have procedures that are of the form:
Get (
Item => X) ;
Get (
File => Some_File,
Item => X) ;
Get (
From => Some_String,
Item => X,
Last => Some_Positive) ;
(Similar services available for Put procedures, but with more options for formatting the output)
Pre-instantiated packages exist for the standard numeric types with names like Ada.Integer_Text_IO and Ada.Float_Text_IO, but there is some dependency on implementation. (Some compilers may have an Ada.Long_Long_Float_Text_IO and others, not. See the ARM and your compiler documentation.)
References:
ARM 3.5 (11), ARM A.10, ARM K (88-91), ARM K (264-267)
(Marin David Condic) |