Difference between revisions of "ReNamer:Scripts:Convert file content from ANSI to UTF-8"
Jump to navigation
Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>") |
m (Text replacement - "<source>" to "<syntaxhighlight lang="pascal">") |
||
Line 13: | Line 13: | ||
Author: Denis Kozlov. Date: 12 February 2014. | Author: Denis Kozlov. Date: 12 February 2014. | ||
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
DataAnsi, DataUTF8, UTF8BOM: AnsiString; | DataAnsi, DataUTF8, UTF8BOM: AnsiString; |
Latest revision as of 15:03, 8 February 2017
Convert the content of processed files from ANSI (default system code page) to UTF-8 encoding.
To prevent files from being processed more than once, we will insert the UTF-8 byte order mark (BOM) "0xEF 0xBB 0xBF
" at the beginning of converted files and check for it before processing the file.
Tested
- ReNamer 5.74.7 Beta
Code
Author: Denis Kozlov. Date: 12 February 2014.
var
DataAnsi, DataUTF8, UTF8BOM: AnsiString;
DataWide: WideString;
begin
UTF8BOM := Chr($EF) + Chr($BB) + Chr($BF);
DataAnsi := FileReadContent(FilePath);
if Copy(DataAnsi, 1, Length(UTF8BOM)) <> UTF8BOM then
begin
DataWide := AnsiToWide(DataAnsi);
DataUTF8 := UTF8BOM + UTF8Encode(DataWide);
FileWriteContent(FilePath, DataUTF8);
end;
end.