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..
Ȳȣ�� 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
> 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
···@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)
"????" <·······@cs.hongik.ac.kr> wrote in message news:<············@hiline.shinbiro.com>...
> I want a function which transform a string into number (Yea same as atoi()
(parse-integer "123")
"????" <·······@cs.hongik.ac.kr> wrote in message news:<············@hiline.shinbiro.com>...
> 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..
See your favorite reference for functions READ-FROM-STRING and PARSE-INTEGER.
--
Eugene.
"????" <·······@cs.hongik.ac.kr> wrote in message news:<············@hiline.shinbiro.com>...
> 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..
http://www.xanalys.com/software_tools/reference/HyperSpec/Body/fun_parse-integer.html
:-)
From: Erik Winkels
Subject: Re: How can I cast a string into integer ?
Date:
Message-ID: <87bskenszw.fsf@xs4all.nl>
Hi,
"Ȳȣ��" <·······@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
From: Clive Tong
Subject: Re: How can I cast a string into integer ?
Date:
Message-ID: <u3d5qywpo.fsf@scientia.com>
"Ȳȣ��" <·······@cs.hongik.ac.kr> writes:
> 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..
See parse-integer
http://www.xanalys.com./software_tools/reference/HyperSpec/Body/fun_parse-integer.html
In article <············@hiline.shinbiro.com>, ·······@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.