From: Carolyn Goodman Plampin
Subject: How do I convert from binary string to integer?
Date: 
Message-ID: <336E9423.54E6@ix.netcom.com>
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.

Any help is appreciated.

Best regards,
Randyl Kent Plampin

From: Erik Naggum
Subject: Re: How do I convert from binary string to integer?
Date: 
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?
From: Tim Bradshaw
Subject: Re: How do I convert from binary string to integer?
Date: 
Message-ID: <ey3911rppbk.fsf@staffa.aiai.ed.ac.uk>
* Erik Naggum wrote:
* 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.

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
From: Duncan Smith
Subject: Re: How do I convert from binary string to integer?
Date: 
Message-ID: <3370ACD7.EF8@flavors.com>
Tim Bradshaw wrote:
> 
> * Erik Naggum wrote:
> * 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.
> 
> 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))))))

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
From: Ken Tilton
Subject: Re: How do I convert from binary string to integer?
Date: 
Message-ID: <336F6148.7CCA@bway.net>
Carolyn Goodman Plampin wrote:
> 
> 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.
> 

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
From: Christopher J. Vogt
Subject: Re: How do I convert from binary string to integer?
Date: 
Message-ID: <vogt-0605971446110001@204.248.25.27>
In article <·············@ix.netcom.com>, Carolyn Goodman Plampin
<········@ix.netcom.com> wrote:

> 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.

-- 
···········@novia.net
Omaha, NE
http://www.novia.net/~vogt/
From: Heiko Kirschke
Subject: Re: How do I convert from binary string to integer?
Date: 
Message-ID: <u3es12ikl.fsf@poet.de>
Carolyn Goodman Plampin <········@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: ··············@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
From: marisal
Subject: Re: How do I convert from binary string to integer?
Date: 
Message-ID: <336F7CA1.1243@wrq.com>
Carolyn Goodman Plampin wrote:
> 
> 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.
> 
> Any help is appreciated.
> 
> Best regards,
> Randyl Kent Plampin
You can set the global variable *read-base* to 2, and convert the string
to a symbol.