For Ada 95, request method to convert single-character string into ASCII integer

  1. Is it true that no version of Ada has such a pre-defined function or procedure?
  2. If so, please answer the subject line.

This is probably what you want Scalar Types

_______Probably not.

I guess this is what you mean, the internal ASCII code of a character.

with Ada.Text_IO;

procedure To_ASCII_Code is
   C : Character;
begin
   Ada.Text_IO.Put_Line ("Enter a character");
   Ada.Text_IO.Get (C);
   Ada.Text_IO.Put_Line
      ("ASCII code of '" & C & "' is " & Positive'Image (Character'Pos (C)));
end To_ASCII_Code;

Thanks for reading the subject line and the response.

If you need it to convert a single character string instead of an item of type Character, you can also do this:

with Ada.Text_IO; use Ada.Text_IO;
procedure To_Ascii_Code is

   A : constant String := "A";
   --  A(A'First) is the first character in the string.
   I : constant Integer := Character'Pos ( A(A'First) );

begin

   Put_Line (Integer'Image (I));

end To_Ascii_Code;

ok, ty. ____________

There’s no reason for such a function, because it is utterly trivial.

Function To_Character( Input : String ) return Character
   with Pre => Input'Length = 1;

Function To_Character( Input : String ) return Character is
   Result : Character renames Input(Input'First);
Begin
  Pragma Assert( Result in ASCII.NUL..ASCII.DEL );
  Return Result;
End To_Character;

But that’s not the question OP asked … and isn’t your assertion unnecessary?

Anyway, now I read OP’s question again, what is an “ASCII integer”?

1 Like

Interpreting the subject line very loosely, I presume the OP wants to convert a string with one ASCII character, for example “7”, to the corresponding integer value in base 10, hence 7.

If that’s correct, then the OP should look into the 'Value atrribute: Integer’Value (“7”) = 7.

See paragraph 52 of section 3.5 in the Ada RM: Scalar Types

The answer desired for unb_str := “7” is 55.

I don’t know what OP means, presumably an obtuse “original poster”.

Pragma assert is ensuring that only 7-bit ASCII is being used; IIRC, there was never any 8-bit ASCII standard that was accepted.

And you’re right, I misread the question, the corret form would be:

Function To_Byre( Input : String ) return Interfaces.Unsigned_8
  with Pre => Input'Length = 1;
--…
Function To_Byre( Input : String ) return Interfaces.Unsigned_8 is
  C : Character renames Input( Input'First );
Begin
  Pragma Assert( C in ASCII.NUL..ASCII.DEL );
  Return Character'Pos( C );
End;

Interesting, I didn’t parse it that way when it was asked, either when I misread the question, and right now. The traditional way of doing this is to use the fact that 0…9 are contiguous in ASCII:

Subtype   Digit is Character range '0'..'9';
Subtype Base_10 is Integer   range  0 .. 9 ;

Function To_Base_10( Input : String ) return Base_10
   with Pre => Input'Length = 1 and then Input(Input'First) in Digit;
--…
Function To_Base_10( Input : String ) return Base_10 is
  C : Character renames Input( Input'First );
Begin
  Return Character'Pos(C) - Character'Pos('0');
End To_Base_10;