BigInt to hex and back to Bigint


#1

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?


#2

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

parse(BigInt, str, 16)

#3

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


#4

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.


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.


#5

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 0s 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.