只是好奇为什么以下代码无法在字符串表示中转换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.
Run Code Online (Sandbox Code Playgroud)
德尔福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;
Run Code Online (Sandbox Code Playgroud)
它可以在Unicode而不是Unicode版本的Delphi中使用.
出错时,返回0.