在delphi中将hex str转换为十进制值

Sim*_*iné 8 delphi hex decimal valueconverter delphi-6

我有一个问题,用Delphi转换整数值的十六进制值的字符串表示.

例如:

当我使用该功能时,$ FC75B6A9D025CB16给我802829546:

Abs(StrToInt64('$FC75B6A9D025CB16'))
Run Code Online (Sandbox Code Playgroud)

但如果我使用Windows的calc程序,结果是:18191647110290852630

所以我的问题是:谁是对的?我,还是钙?

有人有这种问题吗?

who*_*ddy 10

事实上802829546这里显然是错误的.

Calc返回64位无符号值(18191647110290852630d).

Delphi Int64类型使用最高位作为符号:

Int := StrToInt64('$FC75B6A9D025CB16');
Showmessage(IntToStr(Int));
Run Code Online (Sandbox Code Playgroud)

返回-255096963418698986正确的值

如果您需要处理大于64位签名的值,请在此处查看Arnaud的答案.


And*_*and 7

该数字太大,无法表示为带符号的64位数字.

FC75B6A9D025CB16h = 18191647110290852630d
Run Code Online (Sandbox Code Playgroud)

最大可能的带符号64位值是

2^63 - 1 = 9223372036854775807
Run Code Online (Sandbox Code Playgroud)


Sim*_*iné 3

我必须使用名为“DFF Library”的 Delphi 库,因为我在 Delphi6 上工作,并且Uint64该版本中不存在该类型。
主页

这是我将十六进制值字符串转换为十进制值字符串的代码:

您需要UBigIntsV3在您的单位中添加您的用途。

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;
Run Code Online (Sandbox Code Playgroud)