Ada characters are tricky… or not?

Strings in Ada are tricky, and so too are characters – or at least they can be if you don’t use the right function. Remember, ADA is huge. There are a bunch of functions that will turn a string into an integer. The first of these is Integer’Value. Given the declaration:

str : string(1..9);
num : integer;

Then the numeric version of the string stored in str, can be realized using this function:

num := Integer'Value(str);

Now to get the individual digits requires peeling them off one by one, which is somewhat tedious. Another way of doing this is using a function which just returns the ASCII value of a character.

for i in 1..9 loop
  num := Character'Pos(str(i)) - 48;
end loop;

Now, as shown in the example, simply subtracting 48 from the ASCII value gives the actual numeric value.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s