P Dutt
P Dutt

Reputation: 259

Ada95: Converting Integer to Character

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?

Upvotes: 6

Views: 16071

Answers (3)

trashgod
trashgod

Reputation: 205885

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.

Upvotes: 6

Miguel
Miguel

Reputation: 114

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

Upvotes: 4

T.E.D.
T.E.D.

Reputation: 44824

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'.

Upvotes: 1

Related Questions