Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a Memo or RichEdit as UTF 8 text file?

i am using Delphi 7, I wrote a program that get some information from user, combines info from several editboxes and memos and other input components in a memo. How can I save this Memo as a UTF 8 text file? I received some files from last program's users without a solution for utf problem and now, when i open the output file i see ???? (as you know!) instead of different language characters, these are not recoverable, are they?

like image 975
Armin Taghavizad Avatar asked Oct 25 '25 08:10

Armin Taghavizad


1 Answers

Like this:

const
  UTF8BOM: array[0..2] of Byte = ($EF, $BB, $BF);
var
  UTF8Str: UTF8String;
  FS: TFileStream;
begin
  UTF8Str := UTF8Encode(Memo1.Text);

  FS := TFileStream.Create('C:\path to\file.txt', fmCreate);
  try
    FS.WriteBuffer(UTF8BOM[0], SizeOf(UTF8BOM));
    FS.WriteBuffer(PAnsiChar(UTF8Str)^, Length(UTF8Str));
  finally
    FS.Free;
  end;
end;
like image 151
Remy Lebeau Avatar answered Oct 27 '25 00:10

Remy Lebeau