I'm Stumped ...
Author Message
I'm Stumped ...

Quote:

>Could anyone point me in the direction of where
>I could find examples of code or at least explain
>a method to convert a string into an integer?

>Is it possible to take a string with the value :
>   '123'
>and turn it into the integer 123?

The Value attribute is the opposite of the Image attribute:

declare
The_Value : constant Integer := Integer'Value ("123");
begin

You can also instantiate package Ada.Text_IO.Integer_IO, which exports a
subprogram Get to read a value from a string.

declare
The_Value : Integer;
Last : Positive;
begin
Ada.Integer_Text_IO.Get (From => "123", Item => The_Value, Last => Last);

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant

(818) 985-1271

Thu, 14 Oct 1999 03:00:00 GMT
I'm Stumped ...

Is it possible to take a string with the value :
'123'
and turn it into the integer 123?

Any Help would be appreciated :)

First, it is trivial to write code that does this, just write a loop that
starts with zero and traverses the string from the back, multiplying what
you have by 10 and adding in the next digit. It's just 2 or 3 lines of code.

Second, let's see if we can learn how to find this in the RM. Bob Duff
did a great job of the index, so we start from there. Look up string:

String   3.6.3(4), A.1(37)
string type   3.6.3(1)

Hmmm! not much to be found there, well we are trying to convert from
string to integer, so perhaps look up Integer:

Integer   3.5.4(11), 3.5.4(21), A.1(12)

OK, off we go to 3.5.4. Now what we want is operations on integer types,
and we do not find it in 3.5.4, but a little looking in the table of
contents shows us where we are:

3.5 Scalar Types
3.5.1 Enumeration Types
3.5.2 Character Types
3.5.3 Boolean Types
3.5.4 Integer Types
3.5.5 Operations of Discrete Types
3.5.6 Real Types
3.5.7 Floating Point Types
3.5.8 Operations of Floating Point Types
3.5.9 Fixed Point Types
3.5.10 Operations of Fixed Point Types

And ah ha! Operations of Discrete types sounds right -- the reason we did
not find anything specific on operations of integer types is that they are
grouped together with discrete types. OK, let's go visit 3.5.5:

And we find a list of attributes, let's glance through and see if there
is anything useful (in fact how about searching this section for String:

We first find Wide_Image, a little look shows us that goes the other way,
but we are getting warm, search on, also it is for Wide_String. Image is
the next hit, still not what we want.

Searching on, we hit Wide_Value -- ah ha! getting very warm, but still
works on Wide_String, one more search, and voila!

52  S'Value
S'Value denotes a function with the following specification:

53                  function S'Value(Arg : String)
return S'Base

54   This function returns a value given an image of the
value as a String, ignoring any leading or trailing spaces.

And there we have it, so what you are looking for is IntegerVlue. Well
it wasn't immediate (ch 3 is always tough), but we found it. I find it
tremendously useful to have an ASCII version of the RM that I can just
search around with in the editor.

General note: I strongly that everyone peruse annex K of the RM, there are
lots of useful attributes there which are worth knowing about (I keep seeing
Ada 95 programmers who clearly do not know about max and min for example).

Thu, 14 Oct 1999 03:00:00 GMT
I'm Stumped ...

Could anyone point me in the direction of where
I could find examples of code or at least explain
a method to convert a string into an integer?

Is it possible to take a string with the value :
'123'
and turn it into the integer 123?

Any Help would be appreciated :)
--
,----------------------------------------------
| Ben Carter
+----------------------------------------------
|   *                                 ,-_|\
| *   *  Perth, Western Australia    /     \
|    '          Australia            *_,-._/
|   *                                     v
+----------------------------------------------

| WWW    : http://members.tripod.com/~bencarter
'----------------------------------------------

Fri, 15 Oct 1999 03:00:00 GMT
I'm Stumped ...

Quote:

> Could anyone point me in the direction of where
> I could find examples of code or at least explain
> a method to convert a string into an integer?

> Is it possible to take a string with the value :
>    '123'
> and turn it into the integer 123?

Section 3.5 of the LRM discusses Scalar types, including the attribute
'Value:

(52)
S'Value
S'Value denotes a function with the following specification:

(53)
function S'Value(Arg : String)
return S'Base

(54)
This function returns a value given an image of the value as a
String, ignoring any leading or trailing spaces.

There are many other useful attributes in Ada.

Quote:
> Thanks For your time ...
> --
> ,----------------------------------------------
> | Ben Carter
> +----------------------------------------------

You're welcome
--
- Stephe

Fri, 15 Oct 1999 03:00:00 GMT
I'm Stumped ...

Quote:
> Could anyone point me in the direction of where
> I could find examples of code or at least explain
> a method to convert a string into an integer?

(Text_IO.Integer_IO): Get & Put...

Sun, 17 Oct 1999 03:00:00 GMT
I'm Stumped ...

Quote:

> > Could anyone point me in the direction of where
> > I could find examples of code or at least explain
> > a method to convert a string into an integer?

> (Text_IO.Integer_IO): Get & Put...

It's certainly easier to user the inter type's 'Value and 'Image
attributes for this.

Sun, 17 Oct 1999 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages