Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

utf 16 - Inno Setup Pascal Script - Reading UTF-16 file

I have an .inf file exported from Resource Hacker. The file is in UTF-16 LE encoding.

EXTRALARGELEGENDSII_INI TEXTFILE "Data.bin"

LARGEFONTSLEGENDSII_INI TEXTFILE "Data_2.bin"

NORMALLEGENDSII_INI TEXTFILE "Data_3.bin"

THEMES_INI TEXTFILE "Data_4.bin" 

When I load it using the LoadStringFromFile function:

procedure LoadResources;
var
  RESOURCE_INFO: AnsiString;
begin
  LoadStringFromFile(ExpandConstant('{tmp}SKINRESOURCE - INFO.inf'), RESOURCE_INFO);
  Log(String(RESOURCE_INFO));
end;

I am getting this in the Debug Output:

E

Please tell me how to fix this issue.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The file is in the UTF-16 LE encoding.

The LoadStringFromFile does not support any Unicode encoding. It loads the file as is, to a byte array (the AnsiString is effectively used as a byte array).

As the Unicode string (in 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 byte array bitwise to the (Unicode) string. And trim the UTF-16 LE BOM (FEFF).

procedure RtlMoveMemory(Dest: string; Source: PAnsiChar; Len: Integer);
  external 'RtlMoveMemory@kernel32.dll stdcall';

function LoadStringFromUTF16LEFile(FileName: string; var S: string): Boolean;
var
  A: AnsiString;
begin
  Result := LoadStringFromFile(FileName, A);
  if Result then
  begin
    SetLength(S, Length(A) div 2);
    RtlMoveMemory(S, A, Length(S) * 2);
    { Trim BOM, if any }
    if (Length(S) >= 1) and (Ord(S[1]) = $FEFF) then
      Delete(S, 1, 1);
  end;
end;

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...