March 2013
2 posts
Formatting integers in different radixes
format has four directives for formatting integers with different radixes:
~BĀ formats as binary: (format nil "~B" 42) => "101010"
~O formats as octal: (format nil "~O" 42) => "52"
~X formats as hexadecimal: (format nil "~X" 666) => "29A"
~R formats with an arbitrary radix between 2 and 36: (format nil "~36R" 18321) => "E4X"
Each directive takes padding and pad-character options...
Literal syntax for integers
There are several ways to write literal integers with different radixes in Common Lisp. #b… is for binary, #o… is for octal, #x… is for hexadecimal, and #r is for an arbitrary radix from 2 to 36. Section 2.4.8.10 has this example:
#2r11010101 ;Another way of writing 213 decimal
#b11010101 ;Ditto
#b+11010101 ;Ditto ...