delphi中十六进制str到十进制值的转换
我在用Delphi将十六进制值的字符串表示转换为整数值时遇到了一个问题 例如: $FC75B6A9D025CB16在我使用该功能时给我802829546: 但是如果我使用Windows中的calc程序,结果是:18191647110290852630 所以我的问题是:谁是对的?我,还是加州大学delphi中十六进制str到十进制值的转换,delphi,hex,decimal,delphi-6,valueconverter,Delphi,Hex,Decimal,Delphi 6,Valueconverter,我在用Delphi将十六进制值的字符串表示转换为整数值时遇到了一个问题 例如: $FC75B6A9D025CB16在我使用该功能时给我802829546: 但是如果我使用Windows中的calc程序,结果是:18191647110290852630 所以我的问题是:谁是对的?我,还是加州大学 有人已经有这种问题了吗?数字太大,无法表示为有符号的64位数字 FC75B6A9D025CB16h = 18191647110290852630d 可能的最大有符号64位值为 2^63 - 1 = 92
有人已经有这种问题了吗?数字太大,无法表示为有符号的64位数字
FC75B6A9D025CB16h = 18191647110290852630d
可能的最大有符号64位值为
2^63 - 1 = 9223372036854775807
要处理大数字,您需要delphi的外部库
事实上,
802829546
在这里显然是错误的
Calc返回一个64位无符号值(18191647110290852630d
)
Delphi Int64类型使用最高位作为符号:
Int := StrToInt64('$FC75B6A9D025CB16');
Showmessage(IntToStr(Int));
返回正确的值-255096963418698986
如果您需要使用大于64位带符号的值,请签出。我必须使用名为“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;
顺便说一下,很明显802829546不可能是对的。整数的十进制表示不能少于十六进制表示。您使用的是什么delphi版本?在Delphi XE中,我使用您的代码得到255096963418698986,这是预期结果…@Andreas以FC开头-它可能是负数,然后可以更短:-)您需要转换为UInt64,一个无符号值。请参见Arnaud的回答:802829546是截断为32位时的结果。在显示结果之前,是否将其存储为整数或基数?
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;