Convert a string into ascii in Ada

Viewed 274

I am trying to convert small strings into their respective ascii decimal values. Like converting the string "Ag" to "065103".

I tried using

integer_variable : Integer := Integer'Value(Ag);

but that gives me constraint error: bad input for 'Value: "Ag".

Is there something else I can use to make this work? Could I just use enumeration?

1 Answers

Strings in Ada are arrays of Characters, thus if you want to convert String to Integer values you have to do this for each Character separately, by taking its position in enumeration (as suggested in the comment to your question). In your example it could be:

integer_variable1 : Natural := Character'Pos('A');
integer_variable2 : Natural := Character'Pos('g');
Related