介绍
本文章介绍了Delphi XE5 Android手机端转换Ansi字符串,本代码是“浙江-樵夫”开发的一个程序,用来对字符串进行转换,主要代码是:
// 读入Ansi(简体中文)文件...
procedure TForm1.CornerButton3Click(Sender: TObject);
var
Stream: TMemoryStream;
begin
Stream:=TMemoryStream.Create;
Stream.LoadFromFile(TPath.GetPublicPath+'/testfile.txt');
SetLength(Data1,Stream.Size);
Stream.read(Data1,Stream.Size);
memo1.Lines.Add('读入Ansi文件成功!长度='+inttostr(length(Data1))+'字节');
FreeAndNil(Stream);
end;
//
// ANSI(简体中文) --> Default...
procedure TForm1.CornerButton4Click(Sender: TObject);
begin
StringValue:=StringOf(TEncoding.Convert(TEncoding.GetEncoding(936),TEncoding.Default,Data1));
memo1.Lines.Add('转换成手机字符串成功!共'+inttostr(Length(StringValue))+'字符');
memo1.Lines.Add(StringValue);
end;
//
// Default --> Ansi(简体中文)...
procedure TForm1.CornerButton5Click(Sender: TObject);
begin
Data2:=TEncoding.Convert(TEncoding.Default,TEncoding.GetEncoding(936),bytesof(StringValue));
memo1.Lines.Add('转换为Ansi串成功!长度='+inttostr(length(Data2))+'字节');
end;
//
// ANSI写回文件...
procedure TForm1.CornerButton6Click(Sender: TObject);
var
Stream: TMemoryStream;
begin
Stream:=TMemoryStream.Create;
Stream.Write(Data2,length(Data2));
Stream.Position:=0;
Stream.SaveToFile(TPath.GetPublicPath+'/testfile.txt');
FreeAndNil(Stream);
memo1.Lines.Add('ANSI串写回文件成功!');
end;
//
// 清屏...
procedure TForm1.CornerButton7Click(Sender: TObject);
begin
Memo1.Lines.Clear;
end;
//
// 程序启动时,显示编码参数...
procedure TForm1.FormCreate(Sender: TObject);
begin
if TEncoding.Default=TEncoding.Unicode then
memo1.Lines.Add('TEncoding.Default=Unicode');
if TEncoding.Default=TEncoding.ANSI then
memo1.Lines.Add('TEncoding.Default=ANSI');
if TEncoding.Default=TEncoding.UTF7 then
memo1.Lines.Add('TEncoding.Default=UTF7');
if TEncoding.Default=TEncoding.UTF8 then
memo1.Lines.Add('TEncoding.Default=UTF8');
memo1.Lines.Add('系统就绪,请继续测试...');
memo1.Lines.Add('');
end;