Delphi中文本文件Utf-8和Ansi转换

时间:2022-07-20 22:43:33

const
  IS_TEXT_UNICODE_ASCII16 = $1;
  IS_TEXT_UNICODE_REVERSE_ASCII16 = $10;
  IS_TEXT_UNICODE_STATISTICS = $2;
  IS_TEXT_UNICODE_REVERSE_STATISTICS = $20;
  IS_TEXT_UNICODE_CONTROLS = $4;
  IS_TEXT_UNICODE_REVERSE_CONTROLS = $40;
  IS_TEXT_UNICODE_SIGNATURE = $8;
  IS_TEXT_UNICODE_REVERSE_SIGNATURE = $80;
  IS_TEXT_UNICODE_ILLEGAL_CHARS = $100;
  IS_TEXT_UNICODE_ODD_LENGTH = $200;
  IS_TEXT_UNICODE_DBCS_LEADBYTE = $400;
  IS_TEXT_UNICODE_NULL_BYTES = $1000;
  IS_TEXT_UNICODE_UNICODE_MASK = $F;
  IS_TEXT_UNICODE_REVERSE_MASK = $F0;
  IS_TEXT_UNICODE_NOT_UNICODE_MASK = $F00;
  IS_TEXT_UNICODE_NOT_ASCII_MASK = $F000;

function DecordUtf8: String;
var
  MyOpt : Integer;
  Text: String;
begin
  Text := 'WWWWWWWWWWWW'; //可以为从文件中读出来的字符
  MyOpt := IS_TEXT_UNICODE_UNICODE_MASK;
  if IsTextUnicode(PChar(Text), length(Text), @MyOpt) then
    Text:= Utf8Decode(Text);
  Result := Text;
end;