gpt4 book ai didi

delphi - 是否可以将 Comp 类型转换为 Int64 或将 Comp 值显示为带逗号的字符串

转载 作者:行者123 更新时间:2023-12-03 19:11:08 26 4
gpt4 key购买 nike

我在使用 comp 类型的组件 (VCLZip) 中有一个事件,但要将结果显示为字符串,我想我需要将 comp 值转换为 int64,但我找不到这样做的方法。 有没有办法将 comp 值转换为 int64?或者是否有不同的方式将补偿值显示为带逗号的字符串……也许是格式?

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 ] ) );

最佳答案

Comp 类型是整数类型,但它被归类为实数。因此,编译器可能不允许您将其直接转换为 Int64,也不允许对其进行赋值。你必须转换它。尝试使用 Trunc() 将其转换为 Integer 类型。

您也可以尝试使用 absolute 指令让 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;

然后 Actor 开始工作:

Tb := CompRec(TotalBytes).I64;

关于delphi - 是否可以将 Comp 类型转换为 Int64 或将 Comp 值显示为带逗号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8057178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com