# decimal to binary converter code in lisp

856 views

Jan 20, 2015, 11:28:53 PM1/20/15
plz give me
decimal to binary converter code in lisp

### Mauricio

Jan 22, 2015, 10:12:27 AM1/22/15
There are several ways to do this, and some may be more appropriate to what you are attempting to use it for, but here is a recursive approach I used for certain purposes.

(defun dec->bin (n)
"Converts a number to a list contaning its binary representation,
LSB comes first"
(multiple-value-bind (r m) (floor n 2)
(if (= n 0) nil (cons m (dec->bin r)))))

Jan 22, 2015, 11:11:09 AM1/22/15
thanks

--
You received this message because you are subscribed to a topic in the Google Groups "Land of Lisp" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/land-of-lisp/2jhW5wymksc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to land-of-lisp...@googlegroups.com.

### John de la Garza

Jan 23, 2015, 8:42:01 PM1/23/15
On Thu, Jan 22, 2015 at 07:12:27AM -0800, Mauricio wrote:
> (defun dec->bin (n)
> "Converts a number to a list contaning its binary representation,
> LSB comes first"
> (multiple-value-bind (r m) (floor n 2)
> (if (= n 0) nil (cons m (dec->bin r)))))
>

nice

### Pratik Doshi

Feb 25, 2017, 7:48:36 AM2/25/17
to Land of Lisp
Can you please let me know how to take input from user.

I am trying to write code I want to write a function btod to turn a binary number represented as a list of digits into the equivalent decimal number, using reduce and lambda. For example, given the list (1 1 0 1) it should return the decimal number 13.

(reduce (lambda (x y) (+ (* x 2) y)) '(1 1 0 1))

### Edward Kenworthy

Feb 25, 2017, 9:36:02 AM2/25/17
to Pratik Doshi, Land of Lisp

Two main ways, make it a web application (have a look at Hunchentoot and similar). Or from the command line like this from Practical Common Lisp:

(format *query-io* "~a: " prompt)

(force-output *query-io*)

Also see Chapter 6 of Land of Lisp.

Edward

P.S.

Just as a style note, use binary-to-decimal or binary->decimal rather than btod.

Sent from Mail for Windows 10

--
You received this message because you are subscribed to the Google Groups "Land of Lisp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to land-of-lisp...@googlegroups.com.