comp.lang.ada
 help / color / mirror / Atom feed
* Re: I'm Stumped ...
  1997-04-28  0:00 I'm Stumped Ben Carter
@ 1997-04-27  0:00 ` Matthew Heaney
  1997-04-27  0:00 ` Robert Dewar
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 1997-04-27  0:00 UTC (permalink / raw)



In article <33638FBE.715C@usa.net>, Ben Carter <b_c@usa.net> wrote:

>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
<mailto:matthew_heaney@acm.org>
(818) 985-1271




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: I'm Stumped ...
  1997-04-28  0:00 I'm Stumped Ben Carter
  1997-04-27  0:00 ` Matthew Heaney
@ 1997-04-27  0:00 ` Robert Dewar
  1997-04-28  0:00 ` Stephen Leake
  1997-04-30  0:00 ` Gautier
  3 siblings, 0 replies; 6+ messages in thread
From: Robert Dewar @ 1997-04-27  0:00 UTC (permalink / raw)



Ben Carter asks:

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

  Any Help would be appreciated :)
  Thanks For your time ...

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).





^ permalink raw reply	[flat|nested] 6+ messages in thread

* I'm Stumped ...
@ 1997-04-28  0:00 Ben Carter
  1997-04-27  0:00 ` Matthew Heaney
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Ben Carter @ 1997-04-28  0:00 UTC (permalink / raw)




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 :)
Thanks For your time ...
-- 
,----------------------------------------------
| Ben Carter
+----------------------------------------------
|   *                                 ,-_|\  
| *   *  Perth, Western Australia    /     \ 
|    '          Australia            *_,-._/ 
|   *                                     v  
+----------------------------------------------
| E-Mail : b_c@usa.net
| WWW    : http://members.tripod.com/~bencarter
'----------------------------------------------




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: I'm Stumped ...
  1997-04-28  0:00 I'm Stumped Ben Carter
  1997-04-27  0:00 ` Matthew Heaney
  1997-04-27  0:00 ` Robert Dewar
@ 1997-04-28  0:00 ` Stephen Leake
  1997-04-30  0:00 ` Gautier
  3 siblings, 0 replies; 6+ messages in thread
From: Stephen Leake @ 1997-04-28  0:00 UTC (permalink / raw)



Ben Carter wrote:
> 
> 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.

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

You're welcome
-- 
- Stephe




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: I'm Stumped ...
  1997-04-28  0:00 I'm Stumped Ben Carter
                   ` (2 preceding siblings ...)
  1997-04-28  0:00 ` Stephen Leake
@ 1997-04-30  0:00 ` Gautier
  1997-04-30  0:00   ` Mats Weber
  3 siblings, 1 reply; 6+ messages in thread
From: Gautier @ 1997-04-30  0:00 UTC (permalink / raw)



> 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...





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: I'm Stumped ...
  1997-04-30  0:00 ` Gautier
@ 1997-04-30  0:00   ` Mats Weber
  0 siblings, 0 replies; 6+ messages in thread
From: Mats Weber @ 1997-04-30  0:00 UTC (permalink / raw)




Gautier wrote:
> 
> > 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.




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1997-04-30  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-28  0:00 I'm Stumped Ben Carter
1997-04-27  0:00 ` Matthew Heaney
1997-04-27  0:00 ` Robert Dewar
1997-04-28  0:00 ` Stephen Leake
1997-04-30  0:00 ` Gautier
1997-04-30  0:00   ` Mats Weber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox