Formatting integers in different radixes

Джерело:
Common Lisp Tips

Дата публікації:
04/03/2013 04:26

Постійна адреса новини:
http://www.vsinovyny.com/8323841

Formatting integers in different radixes

 

04/03/2013 04:26 // Common Lisp Tips

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 (as well as other options). I define these functions in my init files to quickly dump out hex and bit views of integers:

(defun :hex (value &optional (size 4))
  (format t "~v,'0X" size value))

(defun :bits (value &optional (size 8))
  (format t "~v,'0B" size value))
  
* (:bits 42)
00101010

* (:hex 666)
029A

The lower-level write function accepts a :base argument that specifies a radix to use when printing integers. The default value is taken from *print-base*. For example, (write-to-string 65535 :base 16) => "FFFF". Since write underlies how other standard functions produce output, binding *print-base* will affect pretty much everything.

 

» Читати повністю

 

« Наступна новина з архіву
The usefullness of EQUALP
  Попередня новина з архіву
Literal syntax for integers
»

 

 
© 2026 www.vsinovyny.com