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
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__
> 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.
Thank you very much to all of you who promptly have answered my
questions.
(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
> (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.
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 <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__