Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializing char array with space(#32) not null(#0)

Tags:

delphi

i use delphi 2010. i need to fill the char array with spaces not nulls. i used following code which doesn't work.

procedure TForm1.FormCreate(Sender: TObject);
var
  aCharArray: array[0..9] of Char;
begin
  FillChar(aCharArray, sizeof(aCharArray), #32); // doesn't work
  FillChar(aCharArray, sizeof(aCharArray), ' '); // doesn't work

  Caption := aCharArray;
end;

the caption is printed with strange crosses. in an array of ansichar it works well.

please let me know the reason and the solution.

thanks.

like image 677
yangji Avatar asked Jun 28 '14 12:06

yangji


1 Answers

FillChar is mis-named in Unicode Delphi. It should really be named FillAnsiChar. So you are filling the string with UTF-16 characters having ordinal value $2020, aka U+2020 (DAGGER), †.

Instead you should use StringOfChar:

Caption := StringOfChar(' ', 10);
like image 186
David Heffernan Avatar answered Nov 08 '22 02:11

David Heffernan