How can I cast a string into integer ?

177 views
Skip to first unread message

황호연

unread,
Sep 14, 2001, 3:00:27 AM9/14/01
to
I'm novice in LISP.
I want a function which transform a string into number (Yea same as atoi()
in C)
is there any standard library function which is identical to C atoi() ?
Anyone please help me..


Erik Winkels

unread,
Sep 14, 2001, 3:14:59 AM9/14/01
to
Hi,

"황호연" <hyh...@cs.hongik.ac.kr> writes:
> I want a function which transform a string into number (Yea same as
> atoi() in C)

"parse-integer" is what you are looking for.

[1]> (1+ "666")
*** - argument to 1+ should be a number: "666"
1. Break [2]>
[3]> (1+ (parse-integer "666"))
667

See also:

http://www.xanalys.com/software_tools/reference/HyperSpec/Body/fun_parse-integer.html#parse-integer

The HyperSpec can be downloaded from this page:

http://www.xanalys.com/software_tools/reference/HyperSpec/
(see the bottom of the page)


cheers,
Erik

Frank A. Adrian

unread,
Sep 14, 2001, 3:22:36 AM9/14/01
to
황호연 wrote:

> I'm novice in LISP.
> I want a function which transform a string into number (Yea same as atoi()
> in C)
> is there any standard library function which is identical to C atoi() ?

Well, as the C library function atoi() works on C strings and produces a C
integer, probably there's probably not one that's "identical". You
probably won't get overflow errors, either (i.e., you can parse the string
"876439826926923469238696" into an integer and have it work!).

However, if you're looking for a Lisp function that converts a Lisp string
to a Lisp integer, you might want to have a look at the function
parse-integer. It's much more versatile than the C function, allowing a
radix to be specified, skipping whitespace before and after the string,
allowing better error checking - all-in-all a quite nice function.

faa

Frederic Bastenaire

unread,
Sep 14, 2001, 4:38:27 AM9/14/01
to
> I want a function which transform a string into number (Yea same as atoi()
> in C)
There are many ways to do this, for example :

(read-from-string "123") -> the number 123

Cheers,

FB

Clive Tong

unread,
Sep 14, 2001, 4:59:15 AM9/14/01
to

Software Scavenger

unread,
Sep 14, 2001, 6:15:56 AM9/14/01
to
"????" <hyh...@cs.hongik.ac.kr> wrote in message news:<9ns9t5$eqq$1...@hiline.shinbiro.com>...

> I want a function which transform a string into number (Yea same as atoi()

(parse-integer "123")

Eugene Zaikonnikov

unread,
Sep 14, 2001, 8:44:01 AM9/14/01
to
"????" <hyh...@cs.hongik.ac.kr> wrote in message news:<9ns9t5$eqq$1...@hiline.shinbiro.com>...

See your favorite reference for functions READ-FROM-STRING and PARSE-INTEGER.

--
Eugene.

Espen Vestre

unread,
Sep 14, 2001, 8:46:16 AM9/14/01
to
f...@free.fr (Frederic Bastenaire) writes:

> There are many ways to do this, for example :
>
> (read-from-string "123") -> the number 123

it's better to use parse-integer if parsing an integer is what you
want to do. Some lisps, at least lispworks, will execute parse-integer
significantly faster than read-from-string.
--
(espen)

Nirved

unread,
Sep 14, 2001, 8:59:46 AM9/14/01
to

Kaz Kylheku

unread,
Sep 14, 2001, 1:10:58 PM9/14/01
to
In article <9ns9t5$eqq$1...@hiline.shinbiro.com>, hyh...@cs.hongik.ac.kr wrote:

Note: please use only 7-bit characters in your message header
and body when posting to a Usenet discussion group (or at least
an English-language one).

>I'm novice in LISP.
>I want a function which transform a string into number (Yea same as atoi()
>in C)

You mean, you want a Lisp function that can reformat your hard drive or
make demons fly out of your nose if the number being converted cannot
be represented in the target type? :)

atoi is a dangerous function that should only be used when the
text of the number has been lexically verified to be convertible
without error, because the function has no means of reporting error,
and invokes undefined behavior if the integer is too large or too small.

Instead of atoi and atof, you should consider using the functions
strtol, strtoul and strtod.

Reply all
Reply to author
Forward
0 new messages