I'm working with Clojure on some binary formats in which it makes sense to inspect numbers that are hex formatted (0x10
). How can I configure Clojure to render numbers as hex, by default? This is probably most relevant to me while in a REPL, but I'm also interested in more general mechanisms.
Clojure uses to multi-methods for printing data: print-method
(intended to provide output to be read by humans) and print-dup
(that produces output that can be parsed with read).
Thus for REPL you can simply provide your implementation of print-method for java.lang.Number
s like in the REPL session below:
123
=> 123
(prn 123)
123
=> 123
(defmethod print-method Number
[n ^java.io.Writer w]
(.write w (format "0x%X" n)))
=> #object[clojure.lang.MultiFn 0x52ad430a "clojure.lang.MultiFn@52ad430a"]
123
=> 0x7B
(prn 123)
0x7B
=> nil