# Converting an hexadecimal number returned by a C function to a plain integer in Julia

**URL:** <https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659>\
**Category:** New to Julia\
**Created:** [June 27, 2021, 6:55pm UTC](https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659 "2021-06-27T18:55:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmv/32/26323_2.png) [@mmv](https://discourse.julialang.org/u/mmv)\
**Post date:** [June 27, 2021, 6:55pm UTC](https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659/1 "2021-06-27T18:55:04Z")

</div>

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:

```julia
julia> status = ccall((:MtkFileToBlockRange, mtklib), Cint, (Cstring, Ptr{Cuint}, Ptr{Cuint}), fn, fstblk_ref, lstblk_ref)
0

julia> fstblk_ref
Base.RefValue{UInt32}(0x00000013)

julia> lstblk_ref
Base.RefValue{UInt32}(0x000000a2)

```

I’d like to get plain integers instead, though `convert` does not seem to cooperate:

```julia
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`?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 27, 2021, 7:19pm UTC](https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659/2 "2021-06-27T19:19:17Z")

</div>

```julia
julia> h=Ref(0x00000013)
Base.RefValue{UInt32}(0x00000013)

julia> Int(h[])
19

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 27, 2021, 7:23pm UTC](https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659/3 "2021-06-27T19:23:46Z")

</div>

> [@mmv](#):
>
> ```julia
> julia> fstblk_ref
> Base.RefValue{UInt32}(0x00000013)
> 
> ```

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
julia> fstblk_ref[]
0x00000013

```

Note that this _is_ an integer type, so converting to `Integer` will do nothing:

```julia
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
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
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

```

---

<div class="post-metadata">

**Author:** ![mmv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmv/32/26323_2.png) [@mmv](https://discourse.julialang.org/u/mmv)\
**Post date:** [June 27, 2021, 8:29pm UTC](https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659/4 "2021-06-27T20:29:54Z")

</div>

Thanks to both @oheil and @stevengj for this comprehensive answer. Greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 27, 2021, 8:33pm UTC](https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659/5 "2021-06-27T20:33:14Z")

</div>

Note that these will throw if the value is larger than `typemax(Int)`:

```julia
julia> Int(typemax(UInt))                                             
ERROR: InexactError: check_top_bit(Int64, 18446744073709551615)       
Stacktrace:                                                           
 [1] throw_inexacterror(f::Symbol, #unused#::Type{Int64}, val::UInt64)
   @ Core ./boot.jl:608                                               
 [2] check_top_bit                                                    
   @ ./boot.jl:622 [inlined]                                          
 [3] toInt64                                                          
   @ ./boot.jl:683 [inlined]                                          
 [4] Int64(x::UInt64)                                                 
   @ Core ./boot.jl:758                                               
 [5] top-level scope                                                  
   @ REPL[1]:1                                                        
                                                                      
julia> Signed(typemax(UInt))                                          
ERROR: InexactError: check_top_bit(Int64, 18446744073709551615)       
Stacktrace:                                                           
 [1] throw_inexacterror(f::Symbol, #unused#::Type{Int64}, val::UInt64)
   @ Core ./boot.jl:608                                               
 [2] check_top_bit                                                    
   @ ./boot.jl:622 [inlined]                                          
 [3] toInt64                                                          
   @ ./boot.jl:683 [inlined]                                          
 [4] Int64                                                            
   @ ./boot.jl:758 [inlined]                                          
 [5] Signed(x::UInt64)                                                
   @ Core ./boot.jl:783                                               
 [6] top-level scope                                                  
   @ REPL[2]:1                                                        

```

You can use `reinterpret` to (literally) reinterpret the bits as an `Int`:

```julia
julia> reinterpret(Int, typemax(UInt))
-1                                    

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 27, 2021, 8:43pm UTC](https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659/6 "2021-06-27T20:43:57Z")

</div>

> [@Sukera](#):
>
> You can use `reinterpret` to (literally) reinterpret the bits as an `Int` :

Even shorter is `% Int`, as in `fstblk_ref[] % Int`:

```julia
julia> typemax(UInt) % Int
-1

```

(Or you can just use `Int32` instead of `UInt32` in the `ccall` signature.)

But if the C function is documented as returning a `UInt32` I would tend to just leave it in that form and compute with it directly if possible.)

---

<div class="post-metadata">

**Author:** ![mmv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmv/32/26323_2.png) [@mmv](https://discourse.julialang.org/u/mmv)\
**Post date:** [June 27, 2021, 8:48pm UTC](https://discourse.julialang.org/t/converting-an-hexadecimal-number-returned-by-a-c-function-to-a-plain-integer-in-julia/63659/7 "2021-06-27T20:48:44Z")

</div>

Thanks @Sukera and @stevengj, for these additional contributions.
