From: Priti
Subject: Converting from integer to binary
Date: 
Message-ID: <d677a664.0107191058.dd68fb5@posting.google.com>
Hi,
I am a novice with LISP, I need to convert integer valuse to
equivalent in binary. Is there any function in LISP to do it? I
checked out some online material, they only have logical operations
but no conversions.
The reaon I need to do this, is I need a 16 bit random string. Random
function works on integers, so I thought I could use it and then
convert to its equivalent.
Any light on this matter would be really appreciated.
Thanks,
Priti

From: Kaz Kylheku
Subject: Re: Converting from integer to binary
Date: 
Message-ID: <tN467.660$zb.15617@news1.rdc1.bc.home.com>
In article <···························@posting.google.com>, Priti wrote:
>Hi,
>I am a novice with LISP, I need to convert integer valuse to
>equivalent in binary.

Binary is just a read representation of a value, it's not that value
itself.

>Is there any function in LISP to do it? I
>checked out some online material, they only have logical operations
>but no conversions.
>The reaon I need to do this, is I need a 16 bit random string.

You mean something like the characters "0110100101011110"?

What's wrong with

	(format nil "~B" 12345)

If the second argument of format is nil, then instead of going to
a stream, the output goes into a string that is returned. The format
function has many format specifiers.  Learn all about it in your Common
LISP reference manual.
From: Christopher Stacy
Subject: Re: Converting from integer to binary
Date: 
Message-ID: <uvgkmhfdv.fsf@spacy.Boston.MA.US>
>>>>> On 19 Jul 2001 11:58:35 -0700, Priti  ("Priti") writes:
 Priti> I am a novice with LISP, I need to convert integer valuse to
 Priti> equivalent in binary. Is there any function in LISP to do it? I
 Priti> checked out some online material, they only have logical operations
 Priti> but no conversions.
 Priti> The reaon I need to do this, is I need a 16 bit random string. Random
 Priti> function works on integers, so I thought I could use it and then
 Priti> convert to its equivalent.
 Priti> Any light on this matter would be really appreciated.

Hi,

Your request is a little puzzling; what is it that you are going to
try to do with a "16 bit random string"?  The problem is that in Lisp,
each piece of data has a manifest type, and you cannot access the
underlying memory representation.  This means that strings are not
comprised of bits (or ints), but are rather composed of characters.
This is different from programming in C, where variables access memory, 
and the types of the variables determine how the memory will be understood.  
In Lisp you cannot "cast" some 16 bit quantity and consider it a STRING.
This misunderstanding may be why you cannot find "conversion" functions.

Of course you can take a piece of data and "convert" it by somehow
creating a new piece of data.  Someone gave you the example of taking
an integer and producing a string from it.  Here's that example again,
showing some more possibilities:

    (format nil "decimal: ~D   binary: ~B  nice: ~R" 
	    8 8 8)
    => "decimal: 8   binary: 1000  nice: eight"

You could also go the other way:

    (parse-integer "8")
    => 8

Those are conversions, but I bet this is not what you need.

Since strings don't have any "bits", so there's no such thing as a 
"16 bit string", random or otherwise.  But each character in a string
does have a corresponding numerical code, and there are functions that
will produce integers from characters and vice versa.  It would be
possible to produce some random numbers in the appropriate range,
produce some specified number of characters from those, 
and then put those characters into a string.

    (char-code #\Space)
    => 32

    (code-char (random 12))
    => #\Bell


But I still suspect that this is not what you really want to do.

In order to help you further, please tell us more about what you are
trying to do.  If this is a homework problem, you should mention that.

Chris
From: Kent M Pitman
Subject: Re: Converting from integer to binary
Date: 
Message-ID: <sfwn15ypdcz.fsf@world.std.com>
Christopher Stacy <······@spacy.Boston.MA.US> writes:

> >>>>> On 19 Jul 2001 11:58:35 -0700, Priti  ("Priti") writes:
>  Priti> I am a novice with LISP, I need to convert integer valuse to
>  Priti> equivalent in binary. Is there any function in LISP to do it? I
>  Priti> checked out some online material, they only have logical operations
>  Priti> but no conversions.
>  Priti> The reaon I need to do this, is I need a 16 bit random string. Random
>  Priti> function works on integers, so I thought I could use it and then
>  Priti> convert to its equivalent.
>  Priti> Any light on this matter would be really appreciated.
> 
> Hi,
> 
> Your request is a little puzzling; what is it that you are going to
> try to do with a "16 bit random string"? 

[I'm assuming this isn't homework, or at least that my response won't be
 usable as an answer to homework.]

Funny, my first assumption was that he meant

 (random (expt 2 16))

That is, "how do I make a vector of sixteen random bits?"

But all of the other answers (including yours, Chris) seem equally plausible.

Btw, just for completeness, since you mentioned 
 (parse-integer "8") => 8
you should also mention this works for binary, too:
 (parse-integer "10111" :radix 2) => 23, 5
[The ssecondary value returned is a position in the string and can be safely
ignored.]

So if it's the string stuff one needed, then:
 (defun string-to-binary (string) (values (parse-integer string :radix 2)))
 (defun binary-to-string (integer) (format nil "~B" integer))
 (string-to-binary "10111") => 23
 (binary-to-string 21) => "10111"

He could also have been looking for a bit vector, in which case:
 
 (defun binary-to-bit-vector (num &optional (len (integer-length num)))
   (check-type num (integer 0) "a non-negative integer")
   (let ((vec (make-array len :element-type 'bit)))
     (loop for i below len
           do (setf (sbit vec i) (ldb (byte 1 i) num)))
     vec))

 (defun bit-vector-to-binary (vec)
   (let* ((len (length vec)) (num 0))
     (loop for i below len
           do (setf (ldb (byte 1 i) num) (bit vec i)))
     num))

 ;;; Note low-order bit is leftmost in bit strings
 (binary-to-bit-vector 23) => #*11101  

 (bit-vector-to-binary #*11101) => 23

[btw, does anyone know a faster way to do this?  Feels like we should have
 made a built-in that does this conversion, but I couldn't think of one.]

Using the above definitions, a bit string of n random bits is:

 (defun random-bits (n)
   (binary-to-bit-vector (random (expt 2) n) n))