gpt4 book ai didi

delphi - Delphi 6 可以将 UTF-8 葡萄牙语转换为 WideString 吗?

转载 作者:行者123 更新时间:2023-12-03 18:35:05 26 4
gpt4 key购买 nike

我正在使用德尔福 6。

我想将葡萄牙语 UTF-8 编码的字符串解码为 WideString ,但我发现它没有正确解码。

原文为"ANÁLISE8" .使用后UTF8Decode() ,结果为 "ANALISE8" . "A" 顶部的符号消失。

这是代码:

var
f : textfile;
s : UTF8String;
w, test : WideString;
begin
while not eof(f) do
begin
readln(f,s);
w := UTF8Decode(s);

如何将葡萄牙语 UTF-8 字符串解码为 WideString正确吗?

最佳答案

注意 UTF8Decode() 的实现在 Delphi 6 中是不完整的。具体来说,它不支持编码的 4 字节序列,这是处理 U+FFFF 以上的 Unicode 代码点所必需的。 .这意味着 UTF8Decode()只能解码 UCS-2 范围内的 Unicode 代码点,而不是完整的 Unicode 轨道。从而使UTF8Decode()在 Delphi 6 中基本上没用(一直到 Delphi 2007 - 它最终在 Delphi 2009 中修复)。

尝试使用 Win32 MultiByteToWideChar()代替功能,例如:

uses
..., Windows;

function MyUTF8Decode(const s: UTF8String): WideString;
var
Len: Integer;
begin
Len := MultiByteToWideChar(CP_UTF8, 0, PAnsiChar(s), Length(s), nil, 0);
SetLength(Result, Len);
if Len > 0 then
MultiByteToWideChar(CP_UTF8, 0, PAnsiChar(s), Length(s), PWideChar(Result), Len));
end;

var
f : textfile;
s : UTF8String;
w, test : WideString;
begin
while not eof(f) do
begin
readln(f,s);
w := MyUTF8Decode(s);

话虽如此,您的 ANÁLISE8字符串在 UCS-2 范围内,所以我测试了 UTF8Decode()在 Delphi 6 中,它解码了 ANÁLISE8 的 UTF-8 编码形式正好。我会得出结论:
  • 您的 UTF8String变量不包含 ANÁLISE8 的 UTF-8 编码形式以(字节序列 41 4E C3 81 4C 49 53 45 38 )开头,但包含 ASCII 字符串 ANALISE8相反(字节序列 41 4E 41 4C 49 53 45 38 ),它将按原样解码,因为 ASCII 是 UTF-8 的子集。仔细检查您的文件和 Readln() 的输出.
  • 您的 WideString包含 ANÁLISE8按预期正确,但是您输出/调试它的方式(您没有显示)将其转换为 ANSI,丢失 Á在转换过程中。
  • 关于delphi - Delphi 6 可以将 UTF-8 葡萄牙语转换为 WideString 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46560453/

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