# How to convert a number to an hexadecimal \`bitstring\`?

**URL:** https://discourse.julialang.org/t/how-to-convert-a-number-to-an-hexadecimal-bitstring/98673
**Category:** General Usage
**Tags:** numbers, float, bitstring
**Created:** [May 11, 2023, 10:53am UTC](https://discourse.julialang.org/t/how-to-convert-a-number-to-an-hexadecimal-bitstring/98673 "2023-05-11T10:53:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tokujin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tokujin/32/206778_2.png) [@tokujin](https://discourse.julialang.org/u/tokujin)
#### Post date: [May 11, 2023, 10:53am UTC](https://discourse.julialang.org/t/how-to-convert-a-number-to-an-hexadecimal-bitstring/98673/1 "2023-05-11T10:53:38Z")

</div>

I can do

```julia
string(parse(Int, bitstring(x), base=2), base=16)

```

to convert `bitstring(x)` to an hexadecimal string, but is there another way, one which is simpler and doesn’t trim the leading zeros off of integers?

---

<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: [May 11, 2023, 11:22am UTC](https://discourse.julialang.org/t/how-to-convert-a-number-to-an-hexadecimal-bitstring/98673/2 "2023-05-11T11:22:09Z")

</div>

> [@tokujin](#):
>
> but is there another way, one which is simpler and doesn’t trim the leading zeros off of integers?

Why are you calling `bitstring` at all and then parsing it? Why not just call `string(x, base=16)` directly? If you want leading zeros, you can use the `pad` parameter.

(You can also use `@printf` or `@sprintf` from the Printf stdlib, e.g. `@sprintf("%010x", x)` is similar to `string(x, base=16, pad=10)`. `@printf` also allows you to output directly to an `io` stream without converting to a string first.)

---

<div class="post-metadata">

### Author: ![tokujin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tokujin/32/206778_2.png) [@tokujin](https://discourse.julialang.org/u/tokujin)
#### Post date: [May 11, 2023, 11:24am UTC](https://discourse.julialang.org/t/how-to-convert-a-number-to-an-hexadecimal-bitstring/98673/3 "2023-05-11T11:24:46Z")

</div>

Because `x` is a floating point number and as far as I understand `string` takes only integer values.

---

<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: [May 11, 2023, 11:27am UTC](https://discourse.julialang.org/t/how-to-convert-a-number-to-an-hexadecimal-bitstring/98673/4 "2023-05-11T11:27:11Z")

</div>

> [@tokujin](#):
>
> Because `x` is a floating point number and as far as I understand `string` takes only integer values.

Just do `n = reinterpret(UInt64, x)` first (assuming `x` is a `Float64`). Of course, you can then just do `repr(n)` or `show(n)` directly to see a hex representation.

(In general, you should rarely have to go through a string form to convert between two non-string types.)
