首页 > 其他 > 详细

TEncoding

时间:2015-02-06 11:01:40      阅读:318      评论:0      收藏:0      [点我收藏+]
#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);
}

 

TEncoding

原文:http://www.cnblogs.com/cb168/p/4276408.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!