在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
#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); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论