Delphi 取得TXT文件编码


本文整理自网络,侵删。

 
type
  TTextFormat=(tfAnsi,tfUnicode,tfUnicodeBigEndian,tfUtf8);
const
  TextFormatFlag:array[tfAnsi..tfUtf8] of word=($0000,$FFFE,$FEFF,$EFBB); 

function WordLoHiExchange(w:Word):Word;register;
asm
  XCHG AL, AH
end;

{ TextFormat返回文本编码类型,sText未经处理的文本 }
procedure ReadTextFile(const FileName: string;
  var TextFormat: TTextFormat; var sText:string);
var
  w:Word;
  b:Byte;
begin
  with TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone) do
  try
    Read(w,2);
    w:=WordLoHiExchange(w);//因为是以Word数据类型读取,故高低字节互换
    if w = TextFormatFlag[tfUnicode] then
      TextFormat:= tfUnicode
    else if w = TextFormatFlag[tfUnicodeBigEndian] then
      TextFormat:= tfUnicodeBigEndian
    else if w = TextFormatFlag[tfUtf8] then
    begin
      Read(b,1);//这里要注意一下,UFT-8必须要跳过三个字节。
      TextFormat:=tfUtf8;
    end else
    begin
      TextFormat:=tfANSI;
      Position:=0;
    end;
    SetLength(sText,Size-Position);
    ReadBuffer(sText[1],Size-Position);
  finally
    Free;
  end;
end;

相关阅读 >>

Delphi 设置系统默认打印机

print documents from Delphi - print pdf, doc, xls, html, rtf, docx, txt

Delphi 安卓图像压缩bitmapcompress

Delphi 窗体的位置和高宽度-tform:letf、top、width、height、clientwidth、clientheight

Delphi版多开源码,也就是遍历系统内核对象句柄

Delphi 2009 中的泛型

Delphi启动/停止windows服务,启动类型修改为"自动"

Delphi intraweb 在iis下发布的web.config

Delphi 通过api 隐藏任务栏所有托盘图标

sqlite报错database is locked的解决办法

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



打赏

取消

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

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

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

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

评论

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