How can a text file be converted from ANSI to UTF-8 with Delphi 7?

57,296

Solution 1

The Utf8Encode function takes a WideString string as parameter and returns a Utf-8 string.

Sample:

procedure ConvertANSIFileToUTF8File(AInputFileName, AOutputFileName: TFileName);
var
  Strings: TStrings;
begin
  Strings := TStringList.Create;
  try
    Strings.LoadFromFile(AInputFileName);
    Strings.Text := UTF8Encode(Strings.Text);
    Strings.SaveToFile(AOutputFileName);
  finally
    Strings.Free;
  end;
end;

Solution 2

Take a look at GpTextStream which looks like it works with Delphi 7. It has the ability to read/write unicode files in older versions of Delphi (although does work with Delphi 2009) and should help with your conversion.

Share:
57,296
Kromster
Author by

Kromster

My projects: Knights Province - new RTS game being written from scratch in Delphi. KaM Remake - RTS game remake written from scratch in Delphi. WR/AFC11 modding tools - set of tools to mod racing games by Synetic.

Updated on July 22, 2022

Comments

  • Kromster
    Kromster almost 2 years

    I written a program with Delphi 7 which searches *.srt files on a hard drive. This program lists the path and name of these files in a memo. Now I need convert these files from ANSI to UTF-8, but I haven't succeeded.

    • Miles
      Miles about 15 years
      ANSI isn't really a proper character encoding name; Windows generally uses "ANSI" to mean Windows-1252. stackoverflow.com/questions/701882
    • J-16 SDiZ
      J-16 SDiZ almost 15 years
      @Miles: Windows use "ANSI" to means whatever your locale is. It would be SJIS for japanese windows user; GB2312 for S-Chinese windows user, etc...
    • AlexSC
      AlexSC almost 9 years
      Would you please explain what exactly happen, so you "haven't succeeded"?
  • Admin
    Admin about 15 years
    No, I mean ANSI. Open a txt file.(notepad) File----> save as -------> encoding ------> ANSI or UTF-8 or... ----> SAVE I hope, this helps to see my aim...
  • AlexSC
    AlexSC almost 9 years
    The OP tagged the question as delphi-7. In Delphi 7, strings as ANSU by default, so the strings existing in TStringList are also ANSI. Are you sure this will work?
  • mjn
    mjn almost 9 years
    The question is about converting file content from ANSI to UTF-8, the file names (in the memo field) are a different question iiuc
  • mjn
    mjn almost 9 years
    @AlexSC yes (I assume that the files have been created using the same default ANSI code page which is used by the Delphi program)
  • mg30rg
    mg30rg almost 9 years
    @mjn - No. In the question Yilmaz Ekici wrote about a file list in the memo "This program lists the path and name of these files in a memo." , not about file content. Now (s)he might wanted to ask about file content conversion, but (s)he did not.
  • mjn
    mjn almost 9 years
    1) the question title begins with How can a text file be converted ... 2) after mentioning the file list, the question continues with I need convert these files.