怎样把一个Unicode码的_Txt转换为ansi码的_Txt文件

1.Unicode编码的文件转换成Ansi文件更简单,每次读两个字节,
下面程序提供了双向转换,你只取Uniocde==>Ansi好了
#include "Sysutils.hpp"
void  Ansi2Unicode(String f1,String f2)    // Ansi===> Unicode
{
     TStringList *ss=new TStringList;
     ss->LoadFromFile(f1);
     int fh=FileCreate(f2);
     for (int i=0;iCount;i++)
        {
           String s0=ss->Strings[i];
           s0=s0+"/r/n";
           WideString ws=s0;
           void  *buf=(void *)ws.c_bstr();
           FileWrite(fh,buf,ws.Length()*2);
        }
     FileClose(fh);
     delete ss;
}
void Unicode2Ansi(String f1,String f2)     // Unicode ==> Ansi
{
     int fh1=FileOpen(f1,fmOpenRead);
     int fh2=FileCreate(f2);
     wchar_t  w;
     void  *buf=(void *)&w;
     while (true)
       {
          int l=FileRead(fh1,buf,2);    // 每次读两个字节
          if (l<2)
             break;
          AnsiString s=WideString(w);
          FileWrite(fh2,(void *)s.c_str(),s.Length());
       }
     FileClose(fh1);
     FileClose(fh2);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Ansi2Unicode("c://test1.txt","c://test2.txt");
    ShowMessage("Finished");
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    Unicode2Ansi("c://test2.txt","c://test3.txt");
    ShowMessage("Finished");
}

2.
int WideCharToMultiByte(

    UINT CodePage,	// code page 
    DWORD dwFlags,	// performance and mapping flags 
    LPCWSTR lpWideCharStr,	// address of wide-character string 
    int cchWideChar,	// number of characters in string 
    LPSTR lpMultiByteStr,	// address of buffer for new string 
    int cchMultiByte,	// size of buffer 
    LPCSTR lpDefaultChar,	// address of default for unmappable characters  
    LPBOOL lpUsedDefaultChar 	// address of flag set when default char. used 
   );

你可能感兴趣的:(c/c++)