10

I've got a problem to convert a string representation of an hex value in integer value with Delphi.

for example:

$FC75B6A9D025CB16 give me 802829546 when i use the function:

Abs(StrToInt64('$FC75B6A9D025CB16'))

but if i use the calc program from Windows, the result is: 18191647110290852630

So my question is: who's right? me, or the calc?

Does anybody already have this kind of problem?

kobik
  • 20,439
  • 4
  • 54
  • 115
Simon Mardiné
  • 538
  • 2
  • 7
  • 21
  • 2
    By the way, it's pretty obvious that 802829546 cannot be right. The decimal representation of an integer cannot be of fewer digits than the hexadecimal representation. – Andreas Rejbrand Dec 12 '12 at 14:46
  • what delphi version are you using? In Delphi XE I get 255096963418698986 using your code which is the expected result... – whosrdaddy Dec 12 '12 at 14:50
  • @Andreas starting with FC - it might be negative number, then it can be shorter :-) – Arioch 'The Dec 12 '12 at 14:59
  • You need to convert to UInt64, an unsigned value. See Arnaud's answer here: http://stackoverflow.com/questions/11808825/val-does-not-work-with-uint64 – David Heffernan Dec 12 '12 at 15:12
  • Since the OP considered both 802829546 and 18191647110290852630 as possible answers, I got the impression that we were talking about positive integers. After all, in most applications you know the sign of a value! Also, I guess that in most cases where you present hexadecimal numbers via the GUI, you are using unsigned values. But nevertheless, I made an assumption that wasn't entirely called for. – Andreas Rejbrand Dec 12 '12 at 16:07
  • 4
    802829546 is the result **when truncated to 32 bits**. Did you store the result in an Integer or a Cardinal prior to displaying it? – Rob Kennedy Dec 12 '12 at 18:08
  • @ Rob Kennedy hi, the result is stored in a integer. – Simon Mardiné Dec 13 '12 at 10:30
  • Simon, integer is 32 bit value (Like Rob Kennedy stated) so that is normal then... – whosrdaddy Dec 13 '12 at 15:30

4 Answers4

11

In fact 802829546 is clearly wrong here.

Calc returns a 64bit unsigned value (18191647110290852630d).

Delphi Int64 type uses highest bit as sign:

Int := StrToInt64('$FC75B6A9D025CB16');
Showmessage(IntToStr(Int));

returns value -255096963418698986 which is correct

If you need to work with values larger than 64bit signed, then check out Arnaud's answer here.

Community
  • 1
  • 1
whosrdaddy
  • 11,297
  • 4
  • 41
  • 92
7

The number is too big to be represented as a signed 64-bit number.

FC75B6A9D025CB16h = 18191647110290852630d

The largest possible signed 64-bit value is

2^63 - 1 = 9223372036854775807
Andreas Rejbrand
  • 95,177
  • 8
  • 253
  • 351
3

to work with big numbers you need external library for delphi

Large numbers in Pascal (Delphi)

Community
  • 1
  • 1
shibormot
  • 1,608
  • 1
  • 11
  • 22
3

I had to use a Delphi library named "DFF Library" because I work on Delphi6 and the type Uint64 does not exist in this version.
Main page

Here's my code to transform a string of hexadecimal value to a string of decimal value:

You need to add UBigIntsV3 to your uses in your unit.

function StrHexaToUInt64Str(const stringHexadecimal: String): string;
var
  unBigInteger:TInteger;
begin
  unBigInteger:=TInteger.Create;
  try
    // stringHexadecimal parameter is passed without the '$' symbol
    // ex: stringHexadecimal:='FFAA0256' and not '$FFAA0256'
    unBigInteger.AssignHex(stringHexadecimal);
    //the boolean value determine if we want to add the thousand separator or not.
    Result:=unBigInteger.converttoDecimalString(false);
  finally
    unBigInteger.free;
  end;
end;
numaroth
  • 1,187
  • 3
  • 22
  • 35
Simon Mardiné
  • 538
  • 2
  • 7
  • 21