I have a Byte-Array like so:
(def byte-arr (byte-array (map byte "This is a test"))) ; => #<byte[] [B@63465272>
When calling .toString
I get [B@1b96107b
. Is it possible to override the .toString
-Method of the clojure type [B
to get This is a test
instead, in that case?
It's a bad idea to globally assume all byte arrays are a printable strings, thus the advice to just use the String constructor is correct. That said, you can add new printing globally by type.
Printing functions eventually devolve to invocations of either
print-method
orprint-dup
multimethods, depending on whether*print-dup*
is true. You can add a new method toprint-method
as follows using theprint-sequential
helper function in core_print.clj:Note this just prints a byte array as if it were a vector of bytes: