How to display UInt in decimal instead of hexadecimal

So far, whenever I had to work with UInt variables, I wanted to see their value in decimal. Unfortunately, the REPL displays UInt values in hexadecimal:

julia> a = rand(UInt16, 5)
5-element Array{UInt16,1}:
 0x8128
 0x0dcf
 0x5f0d
 0x5dc1
 0x9beb

Is there a way to permanently change this behavior? I am aware of this temporary solution:

julia> Int.(a)
5-element Array{Int64,1}:
 33064
  3535
 24333
 24001
 39915

(This quickly gets tedious over time and with structs of UInts.)

3 Likes

I’m not sure if it’s a good idea but you could play with this idea:

(The yellow reminds you that they’re a bit fishy…)

6 Likes

Doing this is type piracy. I would say doing so in a repl session/script etc. is okay. But I would not do it in a package.

2 Likes

fishy indeed.

6 Likes
julia> string(0x0A, base = 10)
"10"

If you want to have UInts printed in base 10, I think you will have to overwrite Base.show.

1 Like
Base.show(io::IO, x::T) where {T<:Union{UInt, UInt128, UInt64,
UInt32, UInt16, UInt8}} = Base.print(io, x)
3 Likes

The trick with Int.(arrayUInt) will work only in cases you have bits to spare. For instance converting UInt32 to Int64 or UInt64 to Int128.

Is there another trick which isn’t limited by this? Something like the trick by @Amin_Yahyaabadi yet works for the current print only and not the whole session?

One can use BigInt.(arrayUInt) which should work. I wonder if there are more options.