Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common lisp integer to hex conversion

Is there a similar function to (parse-integer "ff" :radix 16) that will take me back the other way? If I have the int 255 how do I convert it to the string ff?

like image 453
Mike2012 Avatar asked Oct 05 '09 18:10

Mike2012


2 Answers

(write-to-string 255 :base 16)
like image 102
sepp2k Avatar answered Nov 09 '22 12:11

sepp2k


You can also use format with the ~X radix designator:

CL-USER> (format t "~X" 255)
FF
NIL

To get the leading 0x and a minimum width of, say, four padded with zeros, use

CL-USER> (format t "0x~4,'0X" 255)
0x00FF
NIL

To force the digits from 10 to 15 to be lowercase, use the case conversion directive ~( as follows:

CL-USER> (format t "0x~(~4,'0x~)" 255)
0x00ff
NIL
like image 37
seh Avatar answered Nov 09 '22 13:11

seh