在Delphi中,#30005
这类Unicode编码可通过以下方法转换为汉字:
-
直接转换方法
使用WideChar
函数将十进制Unicode值转为字符:ShowMessage(WideChar(30005)); // 显示"电"字:ml-citation{ref="2,9" data="citationList"}
-
批量转换函数
对于连续#
编码字符串(如#30005#23376
):function DecodeDelphiUnicode(const AStr: string): string;
var
i, StartPos: Integer;
CodeStr: string;
begin
Result := '';
i := 1;
while i <= Length(AStr) do
begin
if AStr[i] = '#' then
begin
StartPos := i + 1;
while (i < Length(AStr)) and (AStr[i+1] in ['0'..'9']) do
Inc(i);
CodeStr := Copy(AStr, StartPos, i - StartPos + 1);
if CodeStr <> '' then
Result := Result + WideChar(StrToInt(CodeStr));
end
else
Result := Result + AStr[i];
Inc(i);
end;
end;调用示例:
ShowMessage(DecodeDelphiUnicode('#30005#23376')); // 显示"电子":ml-citation{ref="2,9" data="citationList"}
-
编码原理说明
#
后接十进制Unicode码点(如30005对应十六进制7535)- Delphi默认使用Unicode编码(2009及以上版本)
- 转换时需确保系统字体支持目标字符集
-
常见问题处理
- 若显示乱码,检查源文件编码是否为UTF-812
- 数据库交互时需统一字符集(如GBK/UTF-8)