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

**URL:** <https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291>\
**Category:** General\
**Created:** [October 5, 2024, 11:43pm UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291 "2024-10-05T23:43:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)\
**Post date:** [October 5, 2024, 11:43pm UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/1 "2024-10-05T23:43:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Micronian2](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@Micronian2](https://forum.ada-lang.io/u/Micronian2)\
**Post date:** [October 6, 2024, 6:35am UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/3 "2024-10-06T06:35:00Z")

</div>

This is probably what you want [Scalar Types](https://www.adaic.org/resources/add_content/standards/95lrm/ARM_HTML/RM-3-5.html#I1566)

---

<div class="post-metadata">

**Author:** ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)\
**Post date:** [October 6, 2024, 8:09am UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/4 "2024-10-06T08:09:24Z")

</div>

\_\_\_\_\_\_\_Probably not.

---

<div class="post-metadata">

**Author:** ![mgrojo](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/mgrojo/32/15_2.png) [@mgrojo](https://forum.ada-lang.io/u/mgrojo)\
**Post date:** [October 6, 2024, 9:54am UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/5 "2024-10-06T09:54:01Z")

</div>

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

```ada
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;

```

---

<div class="post-metadata">

**Author:** ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)\
**Post date:** [October 6, 2024, 5:00pm UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/6 "2024-10-06T17:00:21Z")

</div>

Thanks for reading the subject line and the response.

---

<div class="post-metadata">

**Author:** ![AJ-Ianozi](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/aj-ianozi/32/39_2.png) [@AJ-Ianozi](https://forum.ada-lang.io/u/AJ-Ianozi)\
**Post date:** [October 6, 2024, 7:55pm UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/7 "2024-10-06T19:55:44Z")

</div>

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

```ada
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;

```

---

<div class="post-metadata">

**Author:** ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)\
**Post date:** [October 7, 2024, 2:39am UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/8 "2024-10-07T02:39:22Z")

</div>

ok, ty. \_\_\_\_\_\_\_\_\_\_\_\_

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [October 7, 2024, 5:52am UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/9 "2024-10-07T05:52:16Z")

</div>

> [@m8vl4](#):
>
> - Is it true that no version of Ada has such a pre-defined function or procedure?

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

> [@m8vl4](#):
>
> - If so, please answer the subject line. (method to convert single-character string into ASCII integer)

```ada
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;

```

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [October 7, 2024, 7:10am UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/10 "2024-10-07T07:10:18Z")

</div>

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”?

---

<div class="post-metadata">

**Author:** ![DirkCraeynest](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dirkcraeynest/32/94_2.png) [@DirkCraeynest](https://forum.ada-lang.io/u/DirkCraeynest)\
**Post date:** [October 7, 2024, 10:56am UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/11 "2024-10-07T10:56:09Z")

</div>

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](https://www.adaic.org/resources/add_content/standards/22rm/html/RM-3-5.html#I1774)

---

<div class="post-metadata">

**Author:** ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)\
**Post date:** [October 7, 2024, 1:26pm UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/12 "2024-10-07T13:26:59Z")

</div>

The answer desired for unb\_str := “7” is 55.

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

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [October 8, 2024, 12:44am UTC](https://forum.ada-lang.io/t/for-ada-95-request-method-to-convert-single-character-string-into-ascii-integer/1291/13 "2024-10-08T00:44:31Z")

</div>

> [@simonjwright](#):
>
> 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”?

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:

```ada
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;

```

> [@DirkCraeynest](#):
>
> 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.

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:

```ada
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;

```
