组件(VCLZip)中有一个使用comp类型的事件,但要将结果显示为字符串,我认为需要将comp值转换为int64,但我找不到这样做的方法。是否有将comp值转换为int64的方法?或者是否有不同的方法将comp值显示为带有逗号的字符串.也许是格式?
function FormatKBSize( Bytes: Cardinal ): string;
{ Converts a numeric value into a string that represents the number expressed as a size value in kilobytes. }
var
arrSize: array [ 0 .. 255 ] of char;
begin
{ explorer style }
Result := '';
{ same formating used in the Size column of Explorer in detailed mode }
Result := ShLwApi.StrFormatKBSizeW( Bytes, arrSize, Length( arrSize ) - 1 );
end;
procedure TFormMain.VCLZip1StartZipInfo( Sender: TObject; NumFiles: Integer; TotalBytes: Comp;
var EndCentralRecord: TEndCentral; var StopNow: Boolean );
var
Tb: int64;
begin
InfoWin.Lines.Add( '' );
InfoWin.Lines.Add( 'Number of files to be zipped: ' + IntToStr( NumFiles ) + '...' );
Tb := TotalBytes; // <= this will not compile
Tb := Int64(TotalBytes); // <= this will not compile
InfoWin.Lines.Add( 'Total bytes to process: ' + FormatKBSize( Tb ) + '...' );
end;编辑-这似乎有效,但有更好的方法吗?
InfoWin.Lines.Add( Format( '%n', [ TotalBytes ] ) );发布于 2011-11-08 21:37:20
Comp类型是一个整数类型,但它被归类为一个实值。因此,编译器可能不允许直接将其转换为Int64,也不允许将其赋值。你必须改变它。尝试使用Trunc()将其转换为Integer类型。
还可以尝试使用绝对指令使Int64变量与Comp变量共享相同的地址:
procedure TFormMain.VCLZip1StartZipInfo( Sender: TObject; NumFiles: Integer; TotalBytes: Comp;
var EndCentralRecord: TEndCentral; var StopNow: Boolean );
var
Tb: Int64 absolute TotalBytes;虽然我通常不太喜欢它,但它应该可以工作,因为在代码中很容易发现强制转换/转换,如果代码足够长,绝对声明就不容易看到。
第三种解决方案是声明记录:
CompRec = record
I64: Int64;
end;然后演员们的作品:
Tb := CompRec(TotalBytes).I64;发布于 2011-11-08 21:39:15
不管是谁写了VCLZip来使用Comp,都应该为此挨一巴掌。Comp是一个旧的对象Pascal 64位整数类型.作者应该使用Int64代替。甚至(更老的) Delphi文档也说明了这一点:
是Comp (计算)类型,它是Intel CPU的原生类型,代表64位整数。然而,它被归类为真实的,因为它的行为不像序数类型。(例如,不能增加或减少Comp值。)仅为向后兼容性维护Comp。使用Int64类型可以获得更好的性能。
要将Comp转换为Int64,您必须先将Comp转换为Double (编译器确实支持),然后将Double转换为Int64。
https://stackoverflow.com/questions/8057178
复制相似问题