ReNamer:Scripts:Convert file content from ANSI to UTF-8

From den4b Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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.