danbaron
31-05-2011, 07:33
Below are defined two procedures.
"decimal-to-binary", converts a decimal integer to the equivalent binary integer.
"binary-to-decimal", converts a binary integer to the equivalent decimal integer.
:idea: :arrow: :?:
' code -------------------------------------------------------------------------------------------------------------
#lang racket
(define (decimal-to-binary n)
(define binary-list (list))
(define binary-string "")
(define binary-integer 0)
(define bd 0)
(define bc "")
(define (loop n1)
(cond
((= n1 0))
(else
(set! bd (+ (modulo n1 2) 48))
(set! bc (integer->char bd))
(set! binary-list (append binary-list (list bc)))
(loop (floor (/ n1 2))))))
(cond
((= n 0) 0)
(else
(loop n)
(set! binary-list (reverse binary-list))
(set! binary-string (list->string binary-list))
(set! binary-integer (string->number binary-string))))
binary-integer)
(define (binary-to-decimal n)
(define decimal-integer 0)
(define dd 0)
(define binary-string (number->string n))
(define binary-list (string->list binary-string))
(define power (- (length binary-list) 1))
(define (loop l)
(cond
((null? l) decimal-integer)
(else
(set! dd (- (char->integer (car l)) 48))
(when (= dd 1) (set! decimal-integer (+ decimal-integer (expt 2 power))))
(set! power (sub1 power))
(loop (cdr l)))))
(loop binary-list))
' REPL interactions ------------------------------------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (decimal-to-binary 0)
0
> (decimal-to-binary 1)
1
> (decimal-to-binary 2)
10
> (decimal-to-binary 3)
11
> (decimal-to-binary 4)
100
> (decimal-to-binary 5)
101
> (binary-to-decimal 0)
0
> (binary-to-decimal 1)
1
> (binary-to-decimal 10)
2
> (binary-to-decimal 11)
3
> (binary-to-decimal 100)
4
> (binary-to-decimal 101)
5
> (define a 48674657284758692069887631254)
> a
48674657284758692069887631254
> (define b (decimal-to-binary a))
> b
100111010100011010111011100110101010011111111000101001111001011001100101000111101101111110010110
> (define c (binary-to-decimal b))
> c
48674657284758692069887631254
>
"decimal-to-binary", converts a decimal integer to the equivalent binary integer.
"binary-to-decimal", converts a binary integer to the equivalent decimal integer.
:idea: :arrow: :?:
' code -------------------------------------------------------------------------------------------------------------
#lang racket
(define (decimal-to-binary n)
(define binary-list (list))
(define binary-string "")
(define binary-integer 0)
(define bd 0)
(define bc "")
(define (loop n1)
(cond
((= n1 0))
(else
(set! bd (+ (modulo n1 2) 48))
(set! bc (integer->char bd))
(set! binary-list (append binary-list (list bc)))
(loop (floor (/ n1 2))))))
(cond
((= n 0) 0)
(else
(loop n)
(set! binary-list (reverse binary-list))
(set! binary-string (list->string binary-list))
(set! binary-integer (string->number binary-string))))
binary-integer)
(define (binary-to-decimal n)
(define decimal-integer 0)
(define dd 0)
(define binary-string (number->string n))
(define binary-list (string->list binary-string))
(define power (- (length binary-list) 1))
(define (loop l)
(cond
((null? l) decimal-integer)
(else
(set! dd (- (char->integer (car l)) 48))
(when (= dd 1) (set! decimal-integer (+ decimal-integer (expt 2 power))))
(set! power (sub1 power))
(loop (cdr l)))))
(loop binary-list))
' REPL interactions ------------------------------------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (decimal-to-binary 0)
0
> (decimal-to-binary 1)
1
> (decimal-to-binary 2)
10
> (decimal-to-binary 3)
11
> (decimal-to-binary 4)
100
> (decimal-to-binary 5)
101
> (binary-to-decimal 0)
0
> (binary-to-decimal 1)
1
> (binary-to-decimal 10)
2
> (binary-to-decimal 11)
3
> (binary-to-decimal 100)
4
> (binary-to-decimal 101)
5
> (define a 48674657284758692069887631254)
> a
48674657284758692069887631254
> (define b (decimal-to-binary a))
> b
100111010100011010111011100110101010011111111000101001111001011001100101000111101101111110010110
> (define c (binary-to-decimal b))
> c
48674657284758692069887631254
>