gpt4 book ai didi

unicode - Inno Setup 读取 Ansi 和 Unicode 编码的文件

转载 作者:行者123 更新时间:2023-12-04 19:00:17 25 4
gpt4 key购买 nike

我有一个名为 GetServerName 的函数.我需要传递文件名(例如“test.txt”)以及所需的部分字符串(例如“服务器”)
test.txt文件包含这样的东西

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

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

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

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 为 UTF-16LE编码(有点困惑)
enter image description here

最佳答案

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

在 Inno Setup 的 Unicode 版本中, LoadStringsFromFile function默认情况下使用当前的 Windows Ansi 编码。
但是,如果文件有 UTF-8 BOM ,它将相应地处理内容。 BOM 是自动检测 UTF-8(和其他 UTF-*)编码的常用方法。您可以使用 Windows 记事本使用 BOM 创建 UTF-8 编码的文件。
native 不支持 UTF-16 或其他编码。
读取UTF-16文件的实现见Inno Setup Pascal Script - Reading UTF-16 file .
要处理任何编码的文件,包括没有 BOM 的 UTF-8,请参阅 Inno Setup - Convert array of string to Unicode and back to ANSIInno Setup replace a string in a UTF-8 file without BOM .

关于unicode - Inno Setup 读取 Ansi 和 Unicode 编码的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38968230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com