# Easiest way to convert between integer and hex

**URL:** <https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473>\
**Category:** General Usage\
**Created:** [February 5, 2019, 2:31pm UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473 "2019-02-05T14:31:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![srikar\_sastry](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@srikar\_sastry](https://discourse.julialang.org/u/srikar_sastry)\
**Post date:** [February 5, 2019, 2:31pm UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473/1 "2019-02-05T14:31:20Z")

</div>

Hello,

MATLAB provides dec2hex, hex2dec and other such functions to go between different number representations. After searching on the web and Julia documentation, I couldn’t easily get hits on these type of questions (other than implementing our own conversion functions). It seems like Julia 0.6 had a hex function to show the hex representation of integers, but this doesn’t seem to exist on 1.1.

```julia
hex(123)

```

The way to go from integer to hex seems to be

```julia
s = string(12345, base = 16)

```

The hex to Integer seems easier

```julia
Int(0x10)

```

Are there easier/quicker ways to do the decimal to hex conversion? If not, is there a way to request Julia developers to provide functionality similar to MATLAB?

(I could eventually just do this myself and create a local package, but since I’m new, I’m not yet familiar with the proper steps to write custom functions and make them available systemwide.)

Thanks,  
Srikar

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 5, 2019, 2:36pm UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473/2 "2019-02-05T14:36:27Z")

</div>

See the `parse` function.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 5, 2019, 2:51pm UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473/3 "2019-02-05T14:51:59Z")

</div>

To be clear, the only problem here is that you think writing `s = string(12345, base = 16)` is too long? Then the solution is indeed to locally define `hex(s) = string(s, base=16)`.

> [@srikar\_sastry](#):
>
> The hex to Integer seems easier
> 
> ```julia
> Int(0x10)
> 
> ```

This doesn’t convert “hex to integer”. It converts one integer type to another, explicitly, a UInt8 (unsigned 8-bit integer) to the native integer type (likely Int64). `0x10` is just a way of writing `UInt8(16)` and julia shows `UInt8` in hex by default. That is just a choice how to print things to the user and has nothing with what the object is.

---

<div class="post-metadata">

**Author:** ![srikar\_sastry](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@srikar\_sastry](https://discourse.julialang.org/u/srikar_sastry)\
**Post date:** [February 5, 2019, 3:09pm UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473/4 "2019-02-05T15:09:38Z")

</div>

> [@kristoffer.carlsson](#):
>
> To be clear, the only problem here is that you think writing `s = string(12345, base = 16)` is too long?

Correct. 🙂

> [@kristoffer.carlsson](#):
>
> That is just a choice how to print things to the user and has nothing with what the object is.

Yeah, I don’t really need to change the object type. I just need quick ways of simply viewing a value in different number representations in an easy way. The `Int(0x10)` perfectly does this one way. `hex(20)` would be a nice way to view the binary/hex representation of that integer.

> [@kristoffer.carlsson](#):
>
> Then the solution is indeed to locally define `hex(s) = string(s, base=16)` .

Thanks, I’ll proceed with this.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 5, 2019, 8:41pm UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473/5 "2019-02-05T20:41:49Z")

</div>

```julia
julia> h = UInt(1);

julia> 10h
0x000000000000000a

julia> 1234 * 1234h
0x0000000000173c44

```

Your move, MATLAB.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 5, 2019, 9:11pm UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473/6 "2019-02-05T21:11:20Z")

</div>

> [@srikar\_sastry](#):
>
> Yeah, I don’t really need to change the object type. I just need quick ways of simply viewing a value in different number representations in an easy way. The `Int(0x10)` perfectly does this one way. `hex(20)` would be a nice way to view the binary/hex representation of that integer.

The inverse of `string(12345, base = 16)` is `parse(Int, "3039", base = 16)`.

The inverse of `Int(0x10)` is `UInt(16)`.

The two are very different things. Perhaps converting it to `UInt` is sufficient as you’re just interested in the display and unsigned integers display their value in hexadecimal.

---

<div class="post-metadata">

**Author:** ![srikar\_sastry](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@srikar\_sastry](https://discourse.julialang.org/u/srikar_sastry)\
**Post date:** [February 5, 2019, 11:55pm UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473/7 "2019-02-05T23:55:58Z")

</div>

Awesome!!  
So, basically `Unit(xx)` is my built-in `hex` function 🙂

> [@bennedich](#):
>
> Your move, MATLAB.

Haha, sold!!  
Btw, in my initial reference to MATLAB, my intent was never to say MATLAB was easier/better, it was along the lines of what is the easiest workflow to adopt in Julia for what I’d been doing.

---

<div class="post-metadata">

**Author:** ![tienviitta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tienviitta/32/34108_2.png) [@tienviitta](https://discourse.julialang.org/u/tienviitta)\
**Post date:** [March 7, 2022, 10:09am UTC](https://discourse.julialang.org/t/easiest-way-to-convert-between-integer-and-hex/20473/8 "2022-03-07T10:09:44Z")

</div>

I needed to have _signed integers as two’s complement hexadecimal representation_ too so I modified this solution a bit to:

```julia
hex(s; pad=4) = s < 0 ? string(2^(4*pad) + s, base=16, pad=pad) : string(s, base=16, pad=pad)

```

Using ternary operator and `pad` to form the required width.

```julia-repl
julia> hex(23)
"0017"

julia> hex(-23)
"ffe9"

julia> hex(23, pad=2)
"17"

julia> hex(-23, pad=2)
"e9"

```

Of course you should have `pad` greater than `2^ceil(log2(abs(s))/4)`.
