Difference between revisions of "ReNamer:Scripts:Convert file content from ANSI to UTF-8"

From den4b Wiki
Jump to navigation Jump to search
(Created page with "{{Up|ReNamer:Scripts}} 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, w...")
 
m (Text replacement - "<source>" to "<syntaxhighlight lang="pascal">")
 
(One intermediate revision by the same user not shown)
Line 13: Line 13:
 
Author: Denis Kozlov. Date: 12 February 2014.  
 
Author: Denis Kozlov. Date: 12 February 2014.  
  
<source>
+
<syntaxhighlight lang="pascal">
 
var
 
var
 
   DataAnsi, DataUTF8, UTF8BOM: AnsiString;
 
   DataAnsi, DataUTF8, UTF8BOM: AnsiString;
Line 27: Line 27:
 
   end;
 
   end;
 
end.
 
end.
</source>
+
</syntaxhighlight>

Latest revision as of 16: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.