How to save a UTF-16 with BOM file with Inno Setup

天大地大妈咪最大 提交于 2021-02-07 10:28:58

问题


How to save a string to a text file with UTF-16 (UCS-2) encoding with BOM?

The SaveStringsToUTF8File saves as UTF-8.

Using streams saves it as ANSI.

var
  i:integer;
begin
  for i := 1 to length(aString) do begin
    Stream.write(aString[i],1);
    Stream.write(#0,1);
  end;
  stream.free;
end;

回答1:


As the Unicode string (in the Unicode version of Inno Setup – the only version as of Inno Setup 6) actually uses the UTF-16 LE encoding, all you need to do is to copy the (Unicode) string to a byte array (AnsiString) bit-wise. And add the UTF-16 LE BOM (FEFF):

procedure RtlMoveMemoryFromStringToPtr(Dest: PAnsiChar; Source: string; Len: Integer);
  external 'RtlMoveMemory@kernel32.dll stdcall';
  
function SaveStringToUFT16LEFile(FileName: string; S: string): Boolean;
var
  A: AnsiString;
begin
  S := #$FEFF + S; 
  SetLength(A, Length(S) * 2);
  RtlMoveMemoryFromStringToPtr(A, S, Length(S) * 2);
  Result := SaveStringToFile(FileName, A, False);
end;

This is just an opposite of: Inno Setup Pascal Script - Reading UTF-16 file.



来源:https://stackoverflow.com/questions/65060076/how-to-save-a-utf-16-with-bom-file-with-inno-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!