Delphi XE5实现通过TMemoryStream将一个UnicodeString写入到一个Unicode文本文件


本文整理自网络,侵删。

 

本文章介绍了Delphi XE5实现通过TMemoryStream将一个UnicodeString写入到一个Unicode文本文件,之前研究了通过TMemoryStream将UnicodeString写入到文本文件,但是打开后,是乱码,是因为文本文件的字符表示为Ansi,所以写进去打开有问题,当然也可以通过TStrings的SaveToFile来保存成Unicode,但是TMemoStream的SaveToFile没有这样的功能,所以需要提前将Unicode标志写入到文本文件中,下面是代码:


procedure SaveUnicodeA(f:string;s:string);

var

  Buffer, Preamble: TBytes;

  ms:TMemoryStream;

  ws:WideString;

  hs:array[0..1]of char;

begin

  if s='' then exit;

  try

    ms:=TMemoryStream.Create;

    //Buffer := TEncoding.Unicode.GetBytes(s);

    Preamble := TEncoding.Unicode.GetPreamble;

    if Length(Preamble) > 0 then

      ms.WriteBuffer(Preamble, Length(Preamble));

    ms.WriteBuffer(PChar(s)^,length(s)*2);

    ms.SaveToFile(f);

  finally

    ms.Free;

  end;

end;

相关阅读 >>

Delphi 列出所有可视窗口

Delphi 比较俩组mac地址

Delphi 递归算法遍历文件

Delphi datasnap 的连接事件顺序图

Delphi 构建时间 -encodedatetime … tryencodedatetime

Delphi 极速字符串替换函数字符串转16进制

Delphi窗体置顶

Delphi的窗体文件(dfm)文件中的汉字提取出来?

Delphi word转pdf两种方法

Delphi 之 tpagecontrol组件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...