tags:

views:

165

answers:

2
procedure TForm1.Button1Click( Sender: TObject );
var
    arrSize: array[ 0..255 ] of Char;
begin
    {same formating like in statusbar of Explorer}
    StrFormatByteSize( 70846, arrSize, Length( arrSize ) * Sizeof( arrSize) );
    Label1.Caption := 'Result: ' + arrSize;
end;

StrFormatByteSize requires arrsize to be a PWideChar.

How do I get the result to display correctly in Delphi 2009?

+2  A: 

Replace array of Char with array of WideChar, call StrFormatByteSizeW.

sharptooth
+6  A: 

This works fine in Delphi 2009:

procedure TForm1.Button1Click(Sender: TObject);
var
  Buff: UnicodeString;
  TheSize: Int64;
begin
  SetLength(Buff, 255);;
  TheSize := 1024768;
  StrFormatByteSizeW(TheSize, PWideChar(Buff), Length(Buff));
  Label1.Caption := Buff;
end;
Ken White
Please declare Buff as UnicodeString to make clear that "whatever the default string type happens to be" is not appropriate in this case. This API requires a Unicode string. Multiplying by SizeOf(Char) in SetLength is unnecessary. Third StrFormatByteSizeW parameter can be given as Length(Buff)+1.
Rob Kennedy
@Rob: After posting my comment above, I reconsidered about UnicodeString vs. String in the declaration; I agree that specifying UnicodeString makes the intent more clear, and therefore edited accordingly. Thanks for the corrections.
Ken White