Given an arbitrary number of binary characters in a string, say "10000"
or "10010001" as examples, how do I convert this into an integer?
Going from integer to string is easy via format ~b but going in the
other direction is not so obvious to me.
Any help is appreciated.
Best regards,
Randyl Kent Plampin
Carolyn Goodman Plampin <cpla...@ix.netcom.com> writes:
> Given an arbitrary number of binary characters in a string, say "10000"
> or "10010001" as examples, how do I convert this into an integer?
(parse-integer the-string :radix 2)
assuming that the-string contains the string to convert into a
number. parse-integer is a standard function described in Steele:
Common LISP the language, 2nd edition, p. 575.
Viele Gruesse, Heiko
--
Heiko Kirschke EMail: Heiko.K...@poet.de
POET Software GmbH Web: http://www.poet.de/
Fossredder 12 Tel: +49 40 60990-263
D-22359 Hamburg Fax: +49 40 60990-115
just like you would turn any string of digits in any base to an integer.
| Going from integer to string is easy via format ~b but going in the other
| direction is not so obvious to me.
(write <integer>)
will output the integer as a string of digits in the base according to the
value of the special varibale *print-base*. the corresponding special
variable when reading is *read-base*.
(let ((*print-base* 2))
(write 4711))
-> 1001001100111
=> 4711
(setq *print-base* 2)
=> 10
4711
=> 1001001100111
(setq *print-base* 10 *read-base* 2)
=> 2
1001001100111
=> 4711
relevant functions are `read-from-string' and `parse-integer'. note that
`write' and `parse-integer' take keyword arguments that obviate the need to
bind the special variables yourself.
#\Erik
--
if we work harder, will obsolescence be farther ahead or closer?
Just to clarify: are you looking for a built-in capability of Lisp to do
the conversion (can't think of one) or help writing your own function to
do the conversion?
Cheers, Ken
> Greetings lispers:
>
> Given an arbitrary number of binary characters in a string, say "10000"
> or "10010001" as examples, how do I convert this into an integer?
>
> Going from integer to string is easy via format ~b but going in the
> other direction is not so obvious to me.
if you type into a listener: #b10010001 it will tell you (assuming your
base is set to 10). #b is a reader macro, similar to #x for hex. If this
is input from a file, you could set base to 2 and just read the strings
in.
--
mailto:vo...@novia.net
Omaha, NE
http://www.novia.net/~vogt/
> just like you would turn any string of digits in any base to an integer.
Alternatively you could do this (just to make it clear that there's no
magic in PARSE-INTEGER!)
(defun bs-to-int (s)
(declare (type string s))
(let ((len (length s)))
;; ITERATE left as an exercise in case this is a class problem...
(iterate next ((result 0)
(i 0))
(if (= i len)
result
(next (ecase (char s i)
(#\0 result)
(#\1 (+ result (expt 2 (- len i 1)))))
(+ i 1))))))
--tim
Or:
? (format t "~B"
((lambda (s &aux (r 0))
(dotimes (i (length s) r)
(incf r (+ r (ecase (char s i)
(#\0 0)
(#\1 1))))))
"10010001"))
10010001
NIL
Or:
? (format t "~B"
((lambda (s)
(let ((*read-base* 2))
(read-from-string s nil 0)))
"10010001"))
10010001
NIL
?
-Duncan