• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphiTEncoding

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
#include <tchar.h>
#include <memory>       //For STL auto_ptr class
//---------------------------------------------------------------------------
 
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
  // Sample to convert a file of any encoding to UTF8
  TEncoding *LEncoding = NULL;
  std::auto_ptr<TFileStream> LFileStream(new TFileStream("..\\Sample.txt", fmOpenRead));
 
  // Read file into buffer.
  TBytes myBytes;
  std::auto_ptr<TBytesStream> myBytesStream(new TBytesStream(myBytes));
  myBytesStream->CopyFrom(LFileStream.get(), LFileStream->Size);
 
  // Identify encoding and convert buffer to UTF8.
  int LOffset = TEncoding::GetBufferEncoding(myBytesStream->Bytes, LEncoding);
  if (LOffset == 0)
    return 1; // Unknown encoding; do not convert.
  myBytes = TEncoding::Convert(LEncoding, TEncoding::UTF8,
                               myBytesStream->Bytes,
                               LOffset, myBytesStream->Size-LOffset);
  // Create output file.
  std::auto_ptr<TFileStream> DestFileStream(new TFileStream("..\\SampleUTF8.txt", fmCreate));
 
  // Write UTF8 byte-order mark and buffer to output file.
  TBytes LByteOrderMark;
  LByteOrderMark = TEncoding::UTF8->GetPreamble();
 
  // Grab preamble and write to destination.
  DestFileStream->Write(&LByteOrderMark[0], LByteOrderMark.Length);
 
  // Write converted buffer.
  DestFileStream->Write(&myBytes[0], myBytes.Length);
  return 0;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // You may need to change this path to suit your environment.
  String Path = "../../"+Edit1->Text;
  Memo1->Lines->LoadFromFile(Path);
  if (Memo1->Lines->Encoding != NULL)
    Edit2->Text = Memo1->Lines->Encoding->EncodingName;
  Memo1->ScrollBars = ssVertical;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  // You may need to change this path to suit your environment.
  TEncoding *myEncoding;
  String Path = "../../"+Edit1->Text;
  myEncoding = TEncoding::Default;
  if (Edit2->Text == "ASCII") myEncoding = TEncoding::ASCII;
  if (Edit2->Text == "BigEndianUnicode") myEncoding = TEncoding::BigEndianUnicode;
  if (Edit2->Text == "Default") myEncoding = TEncoding::Default;
  if (Edit2->Text == "Unicode") myEncoding = TEncoding::Unicode;
// Do not use UTF7 for this.  It does not have a BOM, and so the encoding cannot be detected on a load.
//  if (Edit2->Text == "UTF7") then myEncoding = TEncoding::UTF7;
  if (Edit2->Text == "UTF8") myEncoding = TEncoding::UTF8;
  Memo1->Lines->SaveToFile(Path, myEncoding);
}

Default是936   (ANSI/OEM - 简体中文 GBK)

  Memo1->Lines->SaveToFile(FileName,TEncoding::UTF8);

 

Memo1->Lines->WriteBOM=false; //utf8  BOM

 

gb2312,它的代码页是936

 

uses System.SysUtils

Str:=  TEncoding.Default.GetString(finallyByte);


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap