Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I convert from binary string to integer?

338 views
Skip to first unread message

Carolyn Goodman Plampin

unread,
May 5, 1997, 3:00:00 AM5/5/97
to

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

Heiko Kirschke

unread,
May 6, 1997, 3:00:00 AM5/6/97
to

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

marisal

unread,
May 6, 1997, 3:00:00 AM5/6/97
to Carolyn Goodman Plampin

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.

Erik Naggum

unread,
May 6, 1997, 3:00:00 AM5/6/97
to

* 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?

Ken Tilton

unread,
May 6, 1997, 3:00:00 AM5/6/97
to

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

Christopher J. Vogt

unread,
May 6, 1997, 3:00:00 AM5/6/97
to

In article <336E94...@ix.netcom.com>, Carolyn Goodman Plampin
<cpla...@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.

--
mailto:vo...@novia.net
Omaha, NE
http://www.novia.net/~vogt/

Tim Bradshaw

unread,
May 7, 1997, 3:00:00 AM5/7/97
to

* 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

Duncan Smith

unread,
May 7, 1997, 3:00:00 AM5/7/97
to

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

0 new messages