How can a text file be converted from ANSI to UTF-8 with Delphi 7?
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.
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, 2022Comments
-
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 about 15 yearsANSI isn't really a proper character encoding name; Windows generally uses "ANSI" to mean Windows-1252. stackoverflow.com/questions/701882
-
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 almost 9 yearsWould you please explain what exactly happen, so you "haven't succeeded"?
-
-
Admin about 15 yearsNo, 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 almost 9 yearsThe 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 almost 9 yearsThe question is about converting file content from ANSI to UTF-8, the file names (in the memo field) are a different question iiuc
-
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 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 almost 9 years1) the question title begins with
How can a text file be converted ...
2) after mentioning the file list, the question continues withI need convert these files
.