Common lisp integer to hex conversion

╄→гoц情女王★ 提交于 2019-12-04 20:31:15

问题


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?


回答1:


(write-to-string 255 :base 16)



回答2:


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


来源:https://stackoverflow.com/questions/1521509/common-lisp-integer-to-hex-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!