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 toolbar按钮添加图标

Delphi简单的播放声音

Delphi里实现文件格式关联应用程序的功能

Delphi滚动窗口标题

Delphi 运行带参数的程序等待其并获取结果

为什么编程是独一无二的职业

Delphi xe版本 运行cmd命令,并取得输出字符

Delphi txt编码互转 ansi utf-8

Delphi xe7上启用android蓝牙

简单的rs232c/ttl电平转换和串口取电

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



打赏

取消

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

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

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

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

评论

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