DELPHI 7 下 TEXT檔 ANSI與UTF8互轉方式
出處:http://tw.myblog.yahoo.com/cschen99ws/article?mid=16596&prev=18175&l=f&fid=52
procedure TForm1.Button1Click(Sender: TObject);
var S:string;
begin
with TMemoryStream.Create do try
S := #$EF#$BB#$BF;
Write(S[1], Length(S));
S := AnsiToUtf8(Memo1.Text);
Write(S[1], Length(S));
Position := 0;
SaveToFile(‘c:\temp.txt’);
finally
Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
S: string;
begin
//取
if not FileExists(‘c:\temp\temp.txt’) then Exit;
with TMemoryStream.Create do try
LoadFromFile(‘c:\temp\temp.txt’);
SetLength(S, Size);
Read(S[1], Length(S));
if Copy(S, 1, 3) <> #$EF#$BB#$BF then Exit;
Memo1.Text := Utf8ToAnsi(Copy(S, 4, MaxInt));
finally
Free;
end;
end;
近期留言