Common lisp integer to hex conversion

后端 未结 2 1016
死守一世寂寞
死守一世寂寞 2021-02-15 16:40

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?

2条回答
  •  感动是毒
    2021-02-15 16:57

    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
    

提交回复
热议问题