Hi! I’m calling a C function from Julia. That function works fine: it returns a status of 0 as expected, and the output parameters are numerically correct, though they are expressed in a hexadecimal format for some reason:
I’d like to get plain integers instead, though convert does not seem to cooperate:
julia> convert(Integer, lstblk_ref)
ERROR: MethodError: Cannot `convert` an object of type Base.RefValue{UInt32} to an object of type Integer
Closest candidates are:
convert(::Type{T}, ::T) where T<:Number at number.jl:6
convert(::Type{T}, ::Number) where T<:Number at number.jl:7
convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at twiceprecision.jl:250
...
Stacktrace:
[1] top-level scope
@ REPL[8]:1
What command or syntax should I use to obtain 162 instead of 0x000000a2?
A Ref like this is analogous to an unsigned * in C — it is essentially a (safe) “pointer” to a UInt32 value. If you want to get the underlying integer value, you need to “dereference the pointer” with the [] operator:
julia> fstblk_ref[]
0x00000013
Note that this is an integer type, so converting to Integer will do nothing:
julia> convert(Integer, fstblk_ref[])
0x00000013
It is printed as hexadecimal simply because that is the default way for Julia to display unsigned integers, which is what this (of type UInt32) is.
If you want to convert it to a “plain” integer, by which I guess you mean a signed integer (which is printed as decimal by default), you can do, for example:
julia> convert(Signed, fstblk_ref[])
19
julia> Signed(fstblk_ref[]) # equivalent:
19
julia> Int(fstblk_ref[]) # a more specific signed type
19
Of course, you can also use fstblk_ref[] directly in most contexts where you might want to work with integer values, rather than converting explicitly. For example:
julia> fstblk_ref[] == 19
true
julia> fstblk_ref[] + 1 # combining with 1::Int promotes result to Int
20
julia> fstblk_ref[] + 0x01 # combining with 0x01::UInt8 gives unsigned result
0x00000014