Subject: Re: How do I convert from binary string to integer?
From: Erik Naggum <erik@naggum.no>
Date: 1997/05/06
Newsgroups: comp.lang.lisp
Message-ID: <3071931359799285@naggum.no>


* Carolyn Goodman Plampin
| Given an arbitrary number of binary characters in a string, say "10000"
| or "10010001" as examples, how do I convert this into an integer?

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?