Forum > General

(Solved) Hex2Dec, strange result

(1/2) > >>

arneolav:
Hi!
I'v to convert some adresses given in hex to decimal, using Hex2Dec
example: 28A11407D6013C81
Result: -704562047
This seems not to be correct or am I wrong?

dseligo:
Use Hex2Dec64 and use QWord as receiving variable type.

Eugene Loza:

28A11407D6013C81 is 2927643256655264897 decimal number, however, Integer can handle only numbers up to 2147483647. This way you are getting an overflow trying to fit a 128-bit number into a 32-bit integer. Note that it won't fit into 64 bit integer either, so you'll have to get creative based on your specific usecase to have number this large to work. E.g. you may want to try https://www.freepascal.org/docs-html/rtl/sysutils/int128rec.html EDIT: I seem to be still sleepy, can't count characters right :) Seems like the number is actually 64 bit, not 128. 16 half-bytes -> 8 bytes -> 64 bits. So no need in 128-bit integers.

A side note: it's always good to run in Debug mode while developing. This way such errors can be caught early. However, I'm not sure if Hex2Dec https://www.freepascal.org/docs-html/rtl/strutils/hex2dec.html actually raises integer overflow error.

dseligo:

--- Quote from: Eugene Loza on June 05, 2023, 07:42:35 am ---Slightly extending dseligo's answer:

28A11407D6013C81 is 2927643256655264897 decimal number, however, Integer can handle only numbers up to 2147483647. This way you are getting an overflow trying to fit a 128-bit number into a 32-bit integer. Note that it won't fit into 64 bit integer either,

--- End quote ---

28A11407D6013C81 is eight bytes so it will fit into 64 bit integer.

Eugene Loza:

--- Quote from: dseligo on June 05, 2023, 07:47:59 am ---28A11407D6013C81 is eight bytes so it will fit into 64 bit integer.

--- End quote ---

Yeah, sorry, just woke up and can't count well :D, corrected the answer