最近总是莫名奇妙的问题。delphi读utf-8编码格式文件

虎滴小猪猪 2011-06-17 11:45:50
function LoadUTF8File(AFileName: string): string;
var
ffileStream:TFileStream;
fAnsiBytes: string;
S: string;
begin
ffileStream:=TFileStream.Create(AFileName,fmOpenRead);
SetLength(S,ffileStream.Size);
ffileStream.Read(S[1],Length(S));
fAnsiBytes:= UTF8Decode(Copy(S,4,MaxInt));
Result:= fAnsiBytes;
end;

这段代码用delphi7读utf-8编码的文件。大部分能读出来。但还是有?,其实?是有字符的。字符上下都有^这种东东。
为什么啊?
...全文
429 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
虎滴小猪猪 2011-06-27
  • 打赏
  • 举报
回复
我在网上看到说memo的组件显示utf8有些可能显示不出来。
fAnsiBytes:= Copy(UTF8Decode(S), 4, MaxInt);

这个转换还是不行。

2010能行是吗?
ADSLAN 2011-06-25
  • 打赏
  • 举报
回复
我现在只用D2010

function LoadUTF8File(AFileName: string): String;
var
begin
SS := TStringStream.Create('', TEncoding.UTF8);
SS.LoadFromFile(AFileName);
Result := SS.DataString;
end;

用字符串流
npkaida 2011-06-24
  • 打赏
  • 举报
回复

function LoadUTF8File(AFileName: string): WideString;
var
ffileStream:TFileStream;
fAnsiBytes: WideString;
S: string;
begin
ffileStream:=TFileStream.Create(AFileName,fmOpenRead);
SetLength(S,ffileStream.Size);
ffileStream.Read(S[1],Length(S));
fAnsiBytes:= UTF8Decode(Copy(S,4,MaxInt));
Result:= fAnsiBytes;
end;


biganchor 2011-06-24
  • 打赏
  • 举报
回复
fAnsiBytes:= UTF8Decode(Copy(S,4,MaxInt));

改为

fAnsiBytes:= Copy(UTF8Decode(S), 4, MaxInt);
虎滴小猪猪 2011-06-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 skylkj 的回复:]
你看下具体是哪个字符会出问题, UTF8Decode转出来的是widestring,也就是unicode编码,你要的是ansistring,是国标编码,这两种编码字符集不完全相同,有些字符utf8里有,但国标里没有,就会出现你说的这种情况
[/Quote]

那我不转换成ansisring。我只是想把utf8的文字在memo里显示出来。
谁知道怎么做?
chu_czx444 2011-06-20
  • 打赏
  • 举报
回复
可能Copy截取时长度的问题
lhy 2011-06-20
  • 打赏
  • 举报
回复
和ascii字符串不兼容吧。
skylkj 2011-06-20
  • 打赏
  • 举报
回复
你看下具体是哪个字符会出问题, UTF8Decode转出来的是widestring,也就是unicode编码,你要的是ansistring,是国标编码,这两种编码字符集不完全相同,有些字符utf8里有,但国标里没有,就会出现你说的这种情况
山东蓝鸟贵薪 2011-06-17
  • 打赏
  • 举报
回复
没试过,不太清楚

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧