Thanks again Karim,
your precious help allowed me to write cleanly what I wanted and I’m looking forward to the possible integration of my MIDI work in the next OM versions. I’ll send you soon the complete documentation I’m making about these extensions.
Friendly
Philippe
;----------------------------------------------------------------------------------------------------------------------
(NUM-TO-BIN (num &optional (k 0))
; Returns the binary representation of a number as a list (by default)
; Depending on the optional argument returns a string (1) or a binary number (2)
(defun num-to-bin (num &optional (k 0))
(cond ((= k 1) (write-to-string num :base 2)) ; → string
((= k 2) (om::string-to-number (write-to-string num :base 2)) ; → number
(t (loop for c across (write-to-string num :base 2) collect (digit-char-p c)) ; → list
))
; (num-to-bin 21) → (1 0 1 0 1)
; (num-to-bin 21 1) → “10101”
; (num-to-bin 21 2) → 10101, 5
;----------------------------------------------------------------------------------------------------------------------
; (BIN-TO-NUM (arg))
; can be a list (1 0 1 0 1), a string “10101” or a binary number 10101.
; Returns the numeric value of the binary representation of
(defun bin-to-num (arg)
(lbin-to-num
(cond ((stringp arg) (loop for c across arg collect (digit-char-p c)))
((integerp arg) (loop for c across (write-to-string arg) collect (digit-char-p c)))
(t arg))))
(defun lbin-to-num (lbin)
(let ((lexp (om::arithm-ser (1- (length lbin)) 0 -1)))
(apply '+ (mapcar (lambda (bin exp) (* bin (expt 2 exp))) lbin lexp))))
; (bin-to-num '(1 0 1 0 1)) → 21
; (bin-to-num “10101”) → 21
; (bin-to-num 10101) → 21