From: Drew Krause
Subject: binary rep as list? string-to-list?
Date: 
Message-ID: <Ohzae.11799$sp3.7191@newsread3.news.atl.earthlink.net>
Hello, I'd like use Lisp to return the binary (base-2) representation of 
a number as a list of integers, e.g. (binaryrep 9) => (1 0 0 1). I've 
gotten as far as the "write-to-string" function,

 > (write-to-string 9 :base 2)
=> "1001"

.. but now stuck on how to convert the "1001" string into a (1 0 0 1) 
integer list. Any help appreciated --- thanks!

From: Zach Beane
Subject: Re: binary rep as list? string-to-list?
Date: 
Message-ID: <m3u0lxi1oz.fsf@unnamed.xach.com>
Drew Krause <········@mindspring.com> writes:

> Hello, I'd like use Lisp to return the binary (base-2) representation
> of a number as a list of integers, e.g. (binaryrep 9) => (1 0 0
> 1). I've gotten as far as the "write-to-string" function,
>
>  > (write-to-string 9 :base 2)
> => "1001"
>
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1)
> integer list. Any help appreciated --- thanks!

You can actually treat an integer directly as a "string" of bits
fairly easily. Here's what I might use if I needed to get the bits of
an integer as a list:

   (defun binary-digit-list (integer)
     (let ((result '()))
       (dotimes (i (integer-length integer) result)
         (push (ldb (byte 1 i) integer) result))))

See also LOGBITP, LOGAND, LOGTEST, etc. Because of these (and related)
functions, I can't think of many situations where I'd use list of bits
in an integer instead of the integer itself.

Zach
From: Ulrich Hobelmann
Subject: Re: binary rep as list? string-to-list?
Date: 
Message-ID: <3d05kiF6qn503U1@individual.net>
Drew Krause wrote:
> Hello, I'd like use Lisp to return the binary (base-2) representation of 
> a number as a list of integers, e.g. (binaryrep 9) => (1 0 0 1). I've 
> gotten as far as the "write-to-string" function,
> 
>  > (write-to-string 9 :base 2)
> => "1001"
> 
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1) 
> integer list. Any help appreciated --- thanks!

Why use a string as intermediate value?  With division and modulo 
you can get the individual numbers and list them as you like.

-- 
No man is good enough to govern another man without that other's 
consent. -- Abraham Lincoln
From: Frank Buss
Subject: Re: binary rep as list? string-to-list?
Date: 
Message-ID: <d4egfe$4e1$1@newsreader3.netcologne.de>
Drew Krause <········@mindspring.com> wrote:

> .. but now stuck on how to convert the "1001" string into a (1 0 0 1) 
> integer list. Any help appreciated --- thanks!

an iterative solution:

(loop for digit across (write-to-string 9 :base 2) collect
      (if (char= digit #\1) 1 0))

or a more functional solution:

(map 'list
     #'(lambda (x) (if (char= x #\1) 1 0))
     (write-to-string 9 :base 2))

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Pascal Bourguignon
Subject: Re: binary rep as list? string-to-list?
Date: 
Message-ID: <87ekd1w69j.fsf@thalassa.informatimago.com>
Drew Krause <········@mindspring.com> writes:

> Hello, I'd like use Lisp to return the binary (base-2) representation
> of a number as a list of integers, e.g. (binaryrep 9) => (1 0 0
> 1). I've gotten as far as the "write-to-string" function,
>
>  > (write-to-string 9 :base 2)
> => "1001"
>
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1)
> integer list. Any help appreciated --- thanks!


(coerce "1010" 'list) 
;; gives a list of _characters_.

(map 'list (lambda (ch) (parse-integer (string ch))) "1010")
;; gives a list of integers.

(let ((string "1010"))
  (loop for i from 0 below (length string)
        collect (parse-integer string :start i :end (1+ i))))
;; too.


;; It might be better to stay with integers when converting bases:
(let ((n 12) (base 2))
  (nreverse (loop for m = n then (truncate m base)
                  until (zerop m)
                  collect (mod m base))))

;; Usually, digits are kept in lists the less significant digit first,
;; (just remove the nreverse).

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
From: Carl Taylor
Subject: Re: binary rep as list? string-to-list?
Date: 
Message-ID: <iSAae.106206$cg1.30190@bgtnsc04-news.ops.worldnet.att.net>
Drew Krause wrote:
> Hello, I'd like use Lisp to return the binary (base-2) representation
> of a number as a list of integers, e.g. (binaryrep 9) => (1 0 0 1).
> I've gotten as far as the "write-to-string" function,
> 
>> (write-to-string 9 :base 2)
> => "1001"
> 
> .. but now stuck on how to convert the "1001" string into a (1 0 0 1)
> integer list. Any help appreciated --- thanks!

Here's a way using a mapping function with <digit-char-p>.

CL-USER 9 > 
(defun explode-integer-into-binary (in-integer)
   (map 'list
        #'digit-char-p
        (write-to-string (the (integer 0 *) in-integer) :base 2)))
EXPLODE-INTEGER-INTO-BINARY

CL-USER 10 > 
(explode-integer-into-binary 217)
(1 1 0 1 1 0 0 1)

Carl Taylor
From: Drew Krause
Subject: Re: binary rep as list? string-to-list?
Date: 
Message-ID: <C%Dae.12384$go4.4404@newsread2.news.atl.earthlink.net>
Many thanks for the great suggestions!

Drew