COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### binary rep as list? string-to-list?

• Follow

```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!
```
 0
Reply drkrause (33) 4/23/2005 9:41:02 PM

```Drew Krause <drkrause@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/
```
 0
Reply pjb (7647) 4/23/2005 9:08:24 PM

```Drew Krause <drkrause@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�, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
```
 0
Reply fb (1526) 4/23/2005 9:59:10 PM

```Drew Krause <drkrause@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))))

functions, I can't think of many situations where I'd use list of bits
in an integer instead of the integer itself.

Zach
```
 0
Reply xach (862) 4/23/2005 10:10:52 PM

```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
```
 0
Reply carltaylor (90) 4/23/2005 11:28:14 PM

```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
```
 0
Reply u.hobelmann (1637) 4/23/2005 11:56:10 PM

```Many thanks for the great suggestions!

Drew
```
 0
Reply drkrause (33) 4/24/2005 3:02:58 AM

6 Replies
35 Views

5/3/2013 5:52:24 AM