11

在 Delphi 7 中,int64 已签名,如果我尝试声明一个大于 $8000000000000000 的十六进制常量(例如,什么是真正的 uint64),我会收到错误消息。你能建议一些解决方法吗?

4

3 回答 3

6

您可以像这样制作变体记录

type muint64 = record
  case boolean of
    true: (i64 : int64);
    false:(lo32, hi32: cardinal);
end;

现在你可以使用基数来用无符号数据填充你的 uint64。

另一种选择是使用如下代码:

const almostmaxint64 = $800000045000000; 
var muint64: int64;    
begin
   muint64:= almostmaxint64;
   muint64:= muint64 shl 1;
end
于 2011-06-17T00:33:02.637 回答
2

Traditionally, Broland implementations suffered interoperability issues because lack of largest unsigned supported by target platform. I remember using LongInt values instead of DWORD and waiting for troubles since very early days of Turbo Pascal for Windows. Then was Cardinal happiness, but no, D4 introduced largest integer Int64 in its signed form only. Again.

So your only option is to rely on signed fundamental type Int64 and pray... wait, no, just use Int64Rec typecast to perform arithmetics on least and most significant part separately.

Back to constant declaration:

const
  foo = $8000004200000001; // this will work because hexadecimal notation is unsigned by its nature
                           // however, declared symbol foo becomes signed Int64 value
                           // attempting to use decimal numeral will result in "Integer constant too large" error
                           // see "True constants" topic in D7 Help for more details

procedure TForm1.FormCreate(Sender: TObject);
begin
  // just to verify
  Caption := IntToHex(foo, SizeOf(Int64) * 2);
end;

Unfortunately, the other workaround is to change your compiler. Free Pascal always keeps signed and unsigned types in sync.


This snippet compiles and yields correct result in Borland Delphi Version 15.0 (a.k.a Delphi 7).

于 2011-06-16T22:51:29.247 回答
2

如果没有编译器的支持,您将没有太多选择。

我假设您希望将值传递给某个外部 DLL 中的函数。您必须将参数声明为带符号的 64 位整数,Int64. 然后,您所能做的就是传入与所需无符号值具有相同位模式的有符号值。使用支持无符号 64 位整数的编译器为自己构建一个小转换器工具。

于 2011-06-16T20:30:46.337 回答