How to define an unsigned 64-bit integer in Delphi7?

前端 未结 3 1982
我寻月下人不归
我寻月下人不归 2020-12-31 11:33

In Delphi 7, int64s are signed, if I try to declare a hex constant larger than $8000000000000000 (eg, what is really an uint64) I get an error. Can you advise some workaroun

3条回答
  •  庸人自扰
    2020-12-31 12:26

    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).

提交回复
热议问题