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

newbie: zero padded binary

4 views
Skip to first unread message

Bigos

unread,
Nov 19, 2009, 8:35:00 AM11/19/09
to
(defun dec2bin (dec)
(setf arg1bin (format nil "~b" dec))
(setf binsize (length arg1bin))
(concatenate 'string (subseq "000000000" 0 (- 8 binsize)) arg1bin))

I want to convert a number into zero padded binary string, eg. convert
7 to "00000111".
Is the code above a right way to do it or is there a better way? I
have checked format documentation but didn't find anything.

also how do I get rid following warning in sbcl?

; compilation unit finished
; Undefined variables:
; ARG1BIN BINSIZE
; caught 2 WARNING conditions

Pascal J. Bourguignon

unread,
Nov 19, 2009, 8:46:10 AM11/19/09
to
Bigos <ruby....@googlemail.com> writes:

Define the variables! Use LET, not SETF.

Also, read the CLHS page for ~B.
http://www.lispworks.com/reference/HyperSpec/Body/22_cbc.htm


(defun padded-binary-representation (integer)
(format nil "~V,'0B" (* 8 (ceiling (integer-length integer) 8)) integer))

(padded-binary-representation 7) --> "00000111"
(padded-binary-representation 2000) --> "0000011111010000"

--
__Pascal Bourguignon__

Peder O. Klingenberg

unread,
Nov 19, 2009, 8:48:41 AM11/19/09
to
Bigos <ruby....@googlemail.com> writes:

> I want to convert a number into zero padded binary string, eg. convert
> 7 to "00000111".

You want prefix arguments. Specifically,
(format nil "~8,'0b" dec)
will replicate your function above.

> also how do I get rid following warning in sbcl?
>
> ; compilation unit finished
> ; Undefined variables:
> ; ARG1BIN BINSIZE
> ; caught 2 WARNING conditions

You are using setf with undeclared global variables. Use let instead.

...Peder...
--
This must be Thursday. I never could get the hang of Thursdays.

Bigos

unread,
Nov 19, 2009, 9:00:49 AM11/19/09
to
On Nov 19, 1:48 pm, pe...@news.klingenberg.no (Peder O. Klingenberg)
wrote:

Thank you very much to all of you who promptly have answered my
questions.

Bigos

unread,
Nov 19, 2009, 9:07:51 AM11/19/09
to

(format t "~8,'0B" 7) does what I need, I will modify my script using
let, but hopefully all my questions have been answered.

Thank you,
Jack

Peder O. Klingenberg

unread,
Nov 19, 2009, 9:47:58 AM11/19/09
to
Bigos <ruby....@googlemail.com> writes:

> (format t "~8,'0B" 7) does what I need, I will modify my script using
> let, but hopefully all my questions have been answered.

Note that this will not pad out to a "round" number of bits for numbers
above 255. Your original will return an error in that case. Pascal B
gave you a more general solution.

Rob Warnock

unread,
Nov 20, 2009, 2:55:45 AM11/20/09
to
Pascal J. Bourguignon <p...@informatimago.com> wrote:
+---------------

| Bigos <ruby....@googlemail.com> writes:
| > (defun dec2bin (dec)
| > (setf arg1bin (format nil "~b" dec))
| > (setf binsize (length arg1bin))
| > (concatenate 'string (subseq "000000000" 0 (- 8 binsize)) arg1bin))
| >
| > I want to convert a number into zero padded binary string, eg. convert
| > 7 to "00000111".
| > Is the code above a right way to do it or is there a better way?
...

| (defun padded-binary-representation (integer)
| (format nil "~V,'0B" (* 8 (ceiling (integer-length integer) 8)) integer))
|
| (padded-binary-representation 7) --> "00000111"
| (padded-binary-representation 2000) --> "0000011111010000"
+---------------

Heh. I suspect he would have been happy with the following simpler version:

(defun padded-binary-representation (integer)
(format nil "~V,'0B" (max 8 (integer-length integer)) integer))

(padded-binary-representation 7) --> "00000111"
(padded-binary-representation 2000) --> "11111010000"


-Rob

-----
Rob Warnock <rp...@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

Pascal J. Bourguignon

unread,
Nov 20, 2009, 10:11:15 AM11/20/09
to
rp...@rpw3.org (Rob Warnock) writes:

> Pascal J. Bourguignon <p...@informatimago.com> wrote:
> +---------------
> | Bigos <ruby....@googlemail.com> writes:
> | > (defun dec2bin (dec)
> | > (setf arg1bin (format nil "~b" dec))
> | > (setf binsize (length arg1bin))
> | > (concatenate 'string (subseq "000000000" 0 (- 8 binsize)) arg1bin))
> | >
> | > I want to convert a number into zero padded binary string, eg. convert
> | > 7 to "00000111".
> | > Is the code above a right way to do it or is there a better way?
> ...
> | (defun padded-binary-representation (integer)
> | (format nil "~V,'0B" (* 8 (ceiling (integer-length integer) 8)) integer))
> |
> | (padded-binary-representation 7) --> "00000111"
> | (padded-binary-representation 2000) --> "0000011111010000"
> +---------------
>
> Heh. I suspect he would have been happy with the following simpler version:
>
> (defun padded-binary-representation (integer)
> (format nil "~V,'0B" (max 8 (integer-length integer)) integer))
>
> (padded-binary-representation 7) --> "00000111"
> (padded-binary-representation 2000) --> "11111010000"


Actually, I wanted to handle signed integers originally:

C/USER[26]> (defun padded-binary-representation (integer)
(let ((width (* 8 (ceiling (integer-length integer) 8))))
(format nil "~V,'0B" width (logand (1- (expt 2 width)) integer))))
PADDED-BINARY-REPRESENTATION
C/USER[27]> (padded-binary-representation -2000)
"1111100000110000"
C/USER[28]> (padded-binary-representation 2000)
"0000011111010000"


--
__Pascal Bourguignon__

0 new messages