gpt4 book ai didi

delphi - Val 不适用于 UInt64?

转载 作者:行者123 更新时间:2023-12-03 15:22:26 25 4
gpt4 key购买 nike

只是好奇为什么以下代码无法将 uint64 值转换为字符串表示形式?

var
num: UInt64;
s: string;
err: Integer;

begin
s := '18446744073709551615'; // High(UInt64)
Val(s, num, err);
if err <> 0 then
raise Exception.Create('Failed to convert UInt64 at ' + IntToStr(err)); // returns 20
end.

德尔福XE2

我在这里遗漏了什么吗?

最佳答案

你是对的:Val()UInt64/QWord 不兼容。

有两个重载函数:

  • 返回浮点值;
  • 返回一个 Int64(即有符号值)。

您可以使用以下代码:

function StrToUInt64(const S: String): UInt64;
var c: cardinal;
P: PChar;
begin
P := Pointer(S);
if P=nil then begin
result := 0;
exit;
end;
if ord(P^) in [1..32] then repeat inc(P) until not(ord(P^) in [1..32]);
c := ord(P^)-48;
if c>9 then
result := 0 else begin
result := c;
inc(P);
repeat
c := ord(P^)-48;
if c>9 then
break else
result := result*10+c;
inc(P);
until false;
end;
end;

它可以在 Unicode 版本和非 Unicode 版本的 Delphi 中运行。

出错时返回 0。

关于delphi - Val 不适用于 UInt64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11808825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com