I have a bit array, e.g.,
(make-array 3 :element-type 'bit :initial-contents '(0 1 0)) => #*010
What I'd like to have is a string, e.g., "010". I could use
string-left-trim and lop off the hash and asterisk, but that's ugly.
Is there a better way?
---
The question above may actually be the wrong question. The real
scenario is that I get three bits (from a database) that are
guaranteed to be binary digits. I need to either 1) print them as a
string, or sometimes 2) print their bit complement as a string. So
what I have now is either (concatenate 'string bit1 bit2 bit3) --
which is easy enough to print, or a (bit-not (make-array ... )) like
the one above. Is a bit-not the right thing to use here?
Thanks in advance,
JT
····@hotmail.com (Jack Tanner) writes:
> I have a bit array, e.g.,
>
> (make-array 3 :element-type 'bit :initial-contents '(0 1 0)) => #*010
>
> What I'd like to have is a string, e.g., "010". I could use
> string-left-trim and lop off the hash and asterisk, but that's ugly.
> Is there a better way?
Why would you have "#*010" in the first place? You have #*010. Just
WRITE-CHAR a #\0 or #\1 to whatever stream you're sending the output to
for each of the bits you read in.
--
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
····@hotmail.com (Jack Tanner) writes:
> I have a bit array, e.g.,
>
> (make-array 3 :element-type 'bit :initial-contents '(0 1 0)) => #*010
>
> What I'd like to have is a string, e.g., "010". I could use
> string-left-trim and lop off the hash and asterisk, but that's ugly.
Ugly and slightly off the mark. #*010 is not a string, it's the
printed represenattion of an object. You could always get it into a
string via WRITE-TO-STRING but that's silly.
> Is there a better way?
How about this:
(defun bits->string (somebits)
(map 'string #'digit-char somebits))
> The question above may actually be the wrong question. The real
> scenario is that I get three bits (from a database) that are
> guaranteed to be binary digits. I need to either 1) print them as a
> string, or sometimes 2) print their bit complement as a string. So
> what I have now is either (concatenate 'string bit1 bit2 bit3) --
> which is easy enough to print, or a (bit-not (make-array ... )) like
> the one above. Is a bit-not the right thing to use here?
Hmmm. if bit1, bit2, and bit3 are actually Lisp bits (i.e. integers
with the value 0 or 1) then that concatenate call is going to fail
because they are not sequences. At any rate, if you're doing bit
twiddling, it seems like bit-not is the thing. Then you can use
bits->string to get a string the way you want.
-Peter
--
Peter Seibel ·····@javamonkey.com
Lisp is the red pill. -- John Fraser, comp.lang.lisp
Hi Jack Tanner,
> I have a bit array, e.g.,
>
> (make-array 3 :element-type 'bit :initial-contents '(0 1 0)) => #*010
>
> What I'd like to have is a string, e.g., "010". I could use
> string-left-trim and lop off the hash and asterisk, but that's ugly.
> Is there a better way?
(defun bit-array->string (bit-array)
(declare (bit-vector bit-array))
(with-output-to-string (out)
(loop for bit of-type bit across bit-array do
(if (zerop bit)
(write-char #\0 out)
(write-char #\1 out)))))
(bit-array->string #*101) => "101"
Depending upon your implementation and specified compilation safety level,
the code may be compiled with or without runtime bit vector type checks.
This is superior to an explicit (typep bit-array 'bit-vector) check as
hard coded type checks cannot be compiled away when choosing to generate
faster but unsafe code.
Also, (format nil "~{~D~}" (coerce #*101 'list)) => "101"
Regards,
Adam
From: Joe Marshall
Subject: Re: need better bit-array-to-string
Date:
Message-ID: <k74yqgjg.fsf@ccs.neu.edu>
····@hotmail.com (Jack Tanner) writes:
> I have a bit array, e.g.,
>
> (make-array 3 :element-type 'bit :initial-contents '(0 1 0)) => #*010
>
> What I'd like to have is a string, e.g., "010". I could use
> string-left-trim and lop off the hash and asterisk, but that's ugly.
> Is there a better way?
>
> ---
>
> The question above may actually be the wrong question. The real
> scenario is that I get three bits (from a database) that are
> guaranteed to be binary digits.
Are they numbers, characters, strings or what? The first paragraph
seems to indicate numbers or a bit-string, the last indicates
characters or strings. Are you only ever going to get three?
One thing to consider is making a vector of the printed
representations and simply indexing into it:
(svref #("000" "001" "010" ...) bits-as-integer)
and for the inverse:
(svref #("111" "110" "101" ...) bits-as-integer)