comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: newbie question: String to Integer conversion
Date: 1999/12/09
Date: 1999-12-09T00:00:00+00:00	[thread overview]
Message-ID: <38503729_4@news1.prserv.net> (raw)
In-Reply-To: OsUkROpQ$GA.331@net003s

In article <OsUkROpQ$GA.331@net003s> , noam@interspective.nl (Noam 
Kloos) wrote:

> I tried to make  a program and it worked. However I also tried to use
> Ada.Command_Line package. My argument must be an integer, but
> Ada.Command_Line passes a String. I do not know how to convert a
> string into an Integer.

All scalar types (including type Integer) come with an attribute

  T'Value

which takes a string and returns a value of the (scalar) type.  See the
appendix re "predefined language attributes" in your RM.

Note also that T'Value accepts a string that has the syntax of a based
literal, something like:

  I := Integer'Value ("16#ABCD#");

Occasionally I meet Ada programmers who don't know this, and proceed to
write their own conversion algorithm.

If this is terminal input, you don't have to require that the user
specify the complete syntax; you can fill it for him:

  Put ("Enter hex value: ");
  Get_Line (Line, Last);

  Value := Integer'Value ("16#" & Line (1 .. Last) & "#");

This way, the user only has to enter "abcd", instead of "16#abcd#".


--
Why stop at evolution and cosmology, though? Let's make sure that the
schoolkids of Kansas get a really first-rate education by loosening up
the teaching standards for other so-called scientific ideas that are,
after all, just theories. The atomic theory, for example. The theory of
relativity. Heck, the Copernican theory--do we really know that the
universe doesn't revolve around the earth?

John Rennie, Scientific American, Oct 1999




      parent reply	other threads:[~1999-12-09  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-09  0:00 newbie question: String to Integer conversion Noam Kloos
1999-12-09  0:00 ` David C. Hoos, Sr.
1999-12-09  0:00 ` Matthew Heaney [this message]
replies disabled

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