< Back to IRCAM Forum

Hexadecimal format

Hi,
is there a Lisp primitive (or very simple) function that allows to convert an integer to hexadecimal format and vice versa, for example :

(integer-to-hexa 21) → (1 0 1 0 1) or 10101
(hexa-to-integer '(1 0 1 0 1) or 10101) → 21

I know how to do this using some of my own functions but I would like to know if there is a more direct way to get the same result.
Thanks in advance,
PB

1 Like

Dear Philip,

You mean here in base 2 and not hex.
if yes, a simple:

(write-to-string 21 :base 2)
=> “10101”

if you need it as a number:

(string-to-number
(write-to-string 21 :base 2))
=> 10101, 5

Now for hex, you can use this:

(format nil “0x~4,'0X” 21)
=> “0x0015”

Best
K

1 Like

Thank you very much dear Karim,
it is exactly that !

However, I miss a simple way to convert a string of type “10101” into a list of numbers (1 0 1 0 1), which would allow me to obtain the inverse function of (lbin-num) that I have already programmed in my own way:

(defun lbin-num (lbin)
(let ((lexp (om::arithm-ser (1- (length lbin)) 0 -1)))
(apply '+ (mapcar (lambda (bin exp) (* bin (expt 2 exp))) lbin lexp))))

(lbin-num '(1 0 1 0 1)) → 21

I guess there is a more elegant solution!

Note: this little preliminary work is part of my ongoing research into extending the MIDI capabilities of OM, including the use of control-changes to alternate or combine timbres (e.g. changing percussion sticks or activating organ registers) in addition to keyswitches to manage articulations.

Many thanks in advance,
Best regards.
Philippe

Dear Philippe,

from the net:

(defun number-to-list (n)
(loop for c across (write-to-string n) collect (digit-char-p c)))

(number-to-list 101001)
=> (1 0 1 0 0 1)

By the way, Thursday i will present some news of the upcoming OM version and will mention your ongoing research and the possibility to integrate some features in OM.

Best
K

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