Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ada95: Converting Integer to Character

Tags:

ada

Is there a way in Ada to convert an Integer to character?

Ex:

TempInt := 1;
InGrid(RowIndex, ColumnIndex) := (ToCharacter(TempInt)); --This will be used to input a character value from an integer into an array of characters.

Is there any "ToCharacter" for Integer->Character conversion for Ada?

like image 470
P Dutt Avatar asked Oct 19 '12 02:10

P Dutt


3 Answers

You may be looking for the 'Val attribute as applied to the discrete subtype Character, illustrated here. Character'Val works like a function that takes an integer and returns a Character.

like image 89
trashgod Avatar answered Oct 19 '22 00:10

trashgod


it depends if you want to convert to the ascii code or if you just want to show the integer value as string.

Here you have an example of both cases

    with Ada.Text_IO;                  use Ada.Text_IO;

    procedure test is
       temp_var : Integer := 97;

    begin
       Put_Line ("Value of the integer shown as string: " & Integer'Image(temp_var));
       Put_Line ("Value of the integer shown as the ascii code: " & Character'Val(temp_var));
    end test;

The result is

Value of the integer shown as string: 97

Value of the integer shown as the ascii code: a

like image 24
Miguel Avatar answered Oct 19 '22 02:10

Miguel


I strongly suggest you look over Annex K of the LRM, as it probably covers what you want, along with a lot of other goodies you don't realise you want yet.

Among the relevant stuff in there:

Converting an integer (Foo) into a printable string representation of that integer's value:

Integer'image(Foo)

Converting an integer (Foo, between 0 and 255) into the ASCII character represented by that value:

Character'Val(Foo)

In the above example, if the value in Foo is 65, then the first line would return the string "65", while the second would return the character 'A'.

like image 43
T.E.D. Avatar answered Oct 19 '22 01:10

T.E.D.