Inno Setup 读取 Ansi 和 Unicode 编码的文件 [英] Inno Setup Reading file in Ansi and Unicode encoding

查看:43
本文介绍了Inno Setup 读取 Ansi 和 Unicode 编码的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 GetServerName 的函数.我需要传递文件名(例如test.txt")以及所需的部分字符串(例如服务器")

I have a function called GetServerName. I need to pass the file name (say for example 'test.txt') as well as a needed section string (say for example 'server')

test.txt 文件包含这样的内容

data1 | abcd
data2 | efgh
server| 'serverName1'
data3 | ijkl

我需要提取服务器名称,因此在我的函数中,我将传递类似 GetServerName('test.txt', 'server') 的内容,它应该返回 serverName1.

I need to extract server name so in my function I will pass something like GetServerName('test.txt', 'server') and it should return serverName1.

我的问题是 test.txt 之前是一个 ANSI 编码的文件.现在它可以是 ANSI 编码的文件或 Unicode 编码的文件.下面的函数对于 ANSI 编码的文件可以正常工作,但是如果文件以 UNICODE 编码,则会出现问题.我怀疑 LoadStringsFromFile 函数有问题.因为当我调试时,我可以看到它返回 Unicode 字符而不是人类可读的字符.如何简单地解决我的问题?(或者如何找到我的文件的编码类型以及如何将UNICODE字符串转换为ANSI进行比较,然后我自己做)

My problem is that the test.txt was an ANSI-encoded file earlier. Now it can be an ANSI-encoded file or Unicode-encoded file. Below function worked correctly for ANSI-encoded file, but giving problem, if file is encoded in UNICODE. I suspect something with LoadStringsFromFile function. Because when, I debug I could see it returns Unicode characters instead of human readable characters. How to solve my issue simply? (or how to find the type of encoding of my file and how to convert UNICODE string to ANSI for comparison, then I can do it myself)

function GetServerName(const FileName, Section: string): string;
//Get Smartlink server name
var
  DirLine: Integer;
  LineCount: Integer;
  SectionLine: Integer;   
  Lines: TArrayOfString;
  //Lines: String;
  AHA: TArrayOfString;
begin
  Result := '';
  if LoadStringsFromFile(FileName, Lines) then
  begin
    LineCount := GetArrayLength(Lines);
    for SectionLine := 0 to LineCount - 1 do
    begin
      AHA := StrSplit(Trim(Lines[SectionLine]), '|')
      if AHA[0] = Section then
      begin
       Result := AHA[1];
       Exit;
      end
    end
  end;
end;

编辑

在 Windows 中,当我另存为文本文件时.我在图像中附加了 4 个选项.我找到了,Windows 提到 unicode 作为

Edit

In Windows, when I Save As text files. I get 4 options as I attached in the image. I found it, Windows mention unicode as UTF-16LE encoding (Bit confusing)

推荐答案

首先,请注意 Unicode 不是一种编码.Unicode 是一个字符集.编码是 UTF-8, UTF-16, UTF-32 等等,所以我们不知道您实际使用的是哪种编码.

First, note that the Unicode is not an encoding. The Unicode is a character set. Encoding is UTF-8, UTF-16, UTF-32 etc. So we do not know which encoding you actually use.

在 Inno Setup 的 Unicode 版本中,LoadStringsFromFile函数默认使用当前的Windows Ansi编码.

In the Unicode version of Inno Setup, the LoadStringsFromFile function uses the current Windows Ansi encoding by default.

但是,如果文件有 UTF-8 BOM,它将相应地处理内容.BOM 是自动检测 UTF-8(和其他 UTF-*)编码的常用方法.您可以使用 Windows 记事本使用 BOM 创建 UTF-8 编码的文件.

But, if the file has the UTF-8 BOM, it will treat the contents accordingly. The BOM is a common way to autodetect the UTF-8 (and other UTF-*) encoding. You can create a file in the UTF-8 encoding with BOM using Windows Notepad.

本机不支持 UTF-16 或其他编码.

UTF-16 or other encodings are not supported natively.

读取UTF-16文件的实现,参见Inno Setup Pascal Script - Reading UTF-16 file.

For implementation of reading UTF-16 file, see Inno Setup Pascal Script - Reading UTF-16 file.

要处理任何编码的文件,包括没有 BOM 的 UTF-8,请参阅 Inno Setup - 将字符串数组转换为 Unicode 并返回到 ANSIInno Setup 替换没有 BOM 的 UTF-8 文件中的字符串.

For working with files in any encoding, including UTF-8 without BOM, see Inno Setup - Convert array of string to Unicode and back to ANSI or Inno Setup replace a string in a UTF-8 file without BOM.

这篇关于Inno Setup 读取 Ansi 和 Unicode 编码的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆