# BigInt to hex and back to Bigint

**URL:** <https://discourse.julialang.org/t/bigint-to-hex-and-back-to-bigint/3873>\
**Category:** General Usage\
**Created:** [May 23, 2017, 7:50am UTC](https://discourse.julialang.org/t/bigint-to-hex-and-back-to-bigint/3873 "2017-05-23T07:50:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyperx](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hyperx](https://discourse.julialang.org/u/hyperx)\
**Post date:** [May 23, 2017, 7:50am UTC](https://discourse.julialang.org/t/bigint-to-hex-and-back-to-bigint/3873/1 "2017-05-23T07:50:44Z")

</div>

Trying to convert hexadecimal strings of VERY big positive numbers back to integers but so far no good.

Tried with hex2num but i get an overflow as number is too big.

hexnum  
“e239f9b3d621a86f8b1ab51cbfa73900d40cc2830fb414dae813e29a20949de0093b395ce2b3a9cd52657fb39ca47ffbdf3a5bd40cb84f2a034ab87f9fcc5cbb3022d9dc180890a8a7f2938215ed9ee121f108d62b481c5d4bc6a9b40cdd3c182e40898920992da08d119075ef5957d07e28f5e766d2a9c4fda2be261fa1746ed1da63491419fce1e7bb323720b5fde8558e0f61a95f1c506d076c4db88321c0f61f7159761041c4e0967d98537355bae2fe2a9fc823ef4ebaa9c6072d6fe3d170b33199702f9c3fca61970aba0284948cc6907bf5b5222adbdd5eda5755bb95e4278c313be16f612256ec705a5edcd0283352af414f00caa21b80ddfe606c645cafc7334cf8cf75a4393297084f44b5f872fa2bdc711cd2bfe304e2101516dbd7c7ce8a87db232e8e760ae008dba1b45cecff7a31c2adfa61d48ef77c622bf488ef1b20c78e2de0be9e7ca23413c29867721082965e1d66e18f2c32e2ffd04448c365972bcebcc305014a948fd0f04bfce60dcb42d190fb03b71c2c352ef02ef84831991a37541ff6839a3a1e6f41d015d226cc169cc1525fccaaf04ba12aebcd9ec87df873fb47f8044dbfee05ecfdfbbf8176a93193f59c5a2745c45a6d62250363937ecd449291f8ecc82082c1442b454f86ff59841310faa300fd4a4fd48297150786421b7c6f8251fd0145e56bb7b801d647fd6c4e4193e3f3c32d8193”

julia.exe --check-bounds=yes --depwarn=yes --math-mode=ieee --optimize test.jl  
ERROR: LoadError: OverflowError()  
in tryparse\_internal(::Type{UInt64}, ::String, ::Int64, ::Int64, ::Int64, ::Bool) at .\parse.jl:114

in parse(::Type{UInt64}, ::String, ::Int64) at .\parse.jl:150  
in hex2num(::String) at .\floatfuncs.jl:40  
in include\_from\_node1(::String) at .\loading.jl:488  
in process\_options(::Base.JLOptions) at .\client.jl:265  
in \_start() at .\client.jl:321

In python I can convert this hex into integer with “int(hexnum, 16)”.  
Any suggestion on how to parse this kind of big integers from and to hex?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [May 23, 2017, 7:54am UTC](https://discourse.julialang.org/t/bigint-to-hex-and-back-to-bigint/3873/2 "2017-05-23T07:54:22Z")

</div>

This seems to do the trick (with `str` your long hex string):

```julia
parse(BigInt, str, 16)

```

---

<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:** [May 23, 2017, 7:56am UTC](https://discourse.julialang.org/t/bigint-to-hex-and-back-to-bigint/3873/3 "2017-05-23T07:56:37Z")

</div>

Perhaps the Base implementation should be more robust to the possible integer overflow?

---

<div class="post-metadata">

**Author:** ![hyperx](https://avatars.discourse-cdn.com/v4/letter/h/b5a626/32.png) [@hyperx](https://discourse.julialang.org/u/hyperx)\
**Post date:** [May 23, 2017, 8:35am UTC](https://discourse.julialang.org/t/bigint-to-hex-and-back-to-bigint/3873/4 "2017-05-23T08:35:28Z")

</div>

Started playing with julia last week and haven’t thought of using parse.Thank you!

In case someone is interested, this is what I have working now.

```julia

function hex2int(hexstring)
  if isodd(length(hexstring))
    throw("hex string must be even")
  end
  local number = BigInt(1)
  try
    number = parse(BigInt, hexstring, 16)
  catch
    throw("ValueError: hex string cannot be converted to integer.")
  end
  return BigInt(number)
end

function int2hex(integer)
  local hexstring = string(hex(integer))
  if isodd(length(hexstring))
    throw("AssertionError: integer conversion produced invalid hex string.")
  end
  return hexstring
end

```

Should do the trick but let me know if you think otherwise.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 23, 2017, 8:52am UTC](https://discourse.julialang.org/t/bigint-to-hex-and-back-to-bigint/3873/5 "2017-05-23T08:52:21Z")

</div>

Your `int2hex` will fail for numbers with an odd number of hex digits (eg `1`). I am not sure this is intended (it is still a valid representation even with an odd number of digits). If you want to pad with `0`s to an even number of digits, try using the second argument of `hex`.

As for `hex2int`, generally it is OK to just let errors propagate up the stack instead of catching and throwing something else.
