Convert hex str to decimal value in delphi

丶灬走出姿态 提交于 2019-12-01 17:13:30
whosrdaddy

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.

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
shibormot

to work with big numbers you need external library for delphi

Large numbers in Pascal (Delphi)

Simon Mardiné

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!