Recent

Author Topic: (Solved) Hex2Dec, strange result  (Read 832 times)

arneolav

  • Full Member
  • ***
  • Posts: 195
    • ElTranslador
(Solved) Hex2Dec, strange result
« on: June 04, 2023, 11:51:38 pm »
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?
« Last Edit: June 05, 2023, 10:15:56 am by arneolav »
Win XP, Win7, Win 10, Win 11, win64 , Lazarus 3.0RC1
Delphi/DevExpress

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: Hex2Dec, strange result
« Reply #1 on: June 05, 2023, 12:05:31 am »
Use Hex2Dec64 and use QWord as receiving variable type.

Eugene Loza

  • Hero Member
  • *****
  • Posts: 656
    • My games in Pascal
Re: Hex2Dec, strange result
« Reply #2 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, 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.
« Last Edit: June 05, 2023, 07:49:15 am by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: Hex2Dec, strange result
« Reply #3 on: June 05, 2023, 07:47:59 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,

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

Eugene Loza

  • Hero Member
  • *****
  • Posts: 656
    • My games in Pascal
Re: Hex2Dec, strange result
« Reply #4 on: June 05, 2023, 07:49:54 am »
28A11407D6013C81 is eight bytes so it will fit into 64 bit integer.

Yeah, sorry, just woke up and can't count well :D, corrected the answer
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

arneolav

  • Full Member
  • ***
  • Posts: 195
    • ElTranslador
Re: Hex2Dec, strange result
« Reply #5 on: June 05, 2023, 10:02:02 am »
Thanks to all,
Hex2Dec64 and QWord (or Int64) did the trick!

But the documentation of Hex2Dec should mention Hex2Dec64!
« Last Edit: June 05, 2023, 10:18:21 am by arneolav »
Win XP, Win7, Win 10, Win 11, win64 , Lazarus 3.0RC1
Delphi/DevExpress

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Hex2Dec, strange result
« Reply #6 on: June 05, 2023, 09:43:52 pm »
But the documentation of Hex2Dec should mention Hex2Dec64!

Well, file a bug against the documentation to have it improved.

 

TinyPortal © 2005-2018