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

纵饮孤独 提交于 2019-11-30 09:12:48

You can make a variant record like so

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

Now you can just use the cardinals to fill your uint64 with unsigned data.

The other option would be to use code like this:

const almostmaxint64 = $800000045000000; 
var muint64: int64;    
begin
   muint64:= almostmaxint64;
   muint64:= muint64 shl 1;
end

Without support from the compiler you don't have many options.

I'm presuming that you wish to pass a value to a function in some external DLL. You'll have to declare the parameter as a signed 64 bit integer, Int64. Then all you can do is pass in the signed value that has the same bit pattern as the desired unsigned value. Build yourself a little converter tool with a compiler that has support for unsigned 64 bit integers.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!