I have an event in a component (VCLZip) that uses the comp type, but to display the result as a string I think I need to convert the comp value to int64, but I can not find a way to do so. Is there a way to convert a comp value to int64? or is there a different way to display a comp value as a string with commas … maybe Format?
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;
Edit – this seems to work but is there a better way?
InfoWin.Lines.Add( Format( '%n', [ TotalBytes ] ) );
The Comp type is an integer type but it is classified as a real. Thereby the compiler may not allow you to cast it directly to an Int64, nor assign it. You have to convert it. Try to use Trunc() to convert it to an Integer type.
You may also try to use the absolute directive to have an Int64 variable share the same address as the Comp variable:
It should work although I usually don’t like it too much because a cast/conversion is easy to spot in code, an absolute declaration may not be seen easily if the code is long enough.
A third solution is to declare a record:
and then the cast works: