【转】几个字符串编码转换实现

  1. #include <stdio.h> 
  2. #include <windows.h> 
  3. #include <locale.h> 
  4. #define BUFF_SIZE 1024 
  5.  
  6. wchar_t * ANSIToUnicode( const char* str ) 
  7.      int textlen ; 
  8.      wchar_t * result; 
  9.      textlen = MultiByteToWideChar( CP_ACP, 0, str,-1, NULL,0 ); 
  10.      result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); 
  11.      memset(result,0,(textlen+1)*sizeof(wchar_t)); 
  12.      MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen ); 
  13.      return result; 
  14.  
  15. char * UnicodeToANSI( const wchar_t* str ) 
  16.      char* result; 
  17.      int textlen; 
  18.      textlen = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL ); 
  19.      result =(char *)malloc((textlen+1)*sizeof(char)); 
  20.      memset( result, 0, sizeof(char) * ( textlen + 1 ) ); 
  21.      WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL ); 
  22.      return result; 
  23.  
  24. wchar_t * UTF8ToUnicode( const char* str ) 
  25.      int textlen ; 
  26.      wchar_t * result; 
  27.      textlen = MultiByteToWideChar( CP_UTF8, 0, str,-1, NULL,0 ); 
  28.      result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); 
  29.      memset(result,0,(textlen+1)*sizeof(wchar_t)); 
  30.      MultiByteToWideChar(CP_UTF8, 0,str,-1,(LPWSTR)result,textlen ); 
  31.      return result; 
  32.  
  33. char * UnicodeToUTF8( const wchar_t* str ) 
  34.      char* result; 
  35.      int textlen; 
  36.      textlen = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL ); 
  37.      result =(char *)malloc((textlen+1)*sizeof(char)); 
  38.      memset(result, 0, sizeof(char) * ( textlen + 1 ) ); 
  39.      WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL ); 
  40.      return result; 
  41. /*宽字符转换为多字符Unicode - ANSI*/ 
  42. char* w2m(const wchar_t* wcs) 
  43.       int len; 
  44.       char* buf; 
  45.       len =wcstombs(NULL,wcs,0); 
  46.       if (len == 0) 
  47.           return NULL; 
  48.       buf = (char *)malloc(sizeof(char)*(len+1)); 
  49.       memset(buf, 0, sizeof(char) *(len+1)); 
  50.       len =wcstombs(buf,wcs,len+1); 
  51.       return buf; 
  52. /*多字符转换为宽字符ANSI - Unicode*/ 
  53. wchar_t* m2w(const char* mbs) 
  54.       int len; 
  55.       wchar_t* buf; 
  56.       len =mbstowcs(NULL,mbs,0); 
  57.       if (len == 0) 
  58.           return NULL; 
  59.       buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1)); 
  60.       memset(buf, 0, sizeof(wchar_t) *(len+1)); 
  61.       len =mbstowcs(buf,mbs,len+1); 
  62.       return buf; 
  63.  
  64. char* ANSIToUTF8(const char* str) 
  65.      return UnicodeToUTF8(ANSIToUnicode(str)); 
  66.  
  67. char* UTF8ToANSI(const char* str) 
  68.      return UnicodeToANSI(UTF8ToUnicode(str)); 
  69.  
  70. int main() 
  71.      /*使用wcstombs和mbstowcs之前必须调用setlocale,以便决定内码*/ 
  72.      setlocale(LC_ALL,".936"); 
  73.      /*假定有一个Unicode(UTF-16LE)编码的文件,将其打开,重新编码为ANSI
  74. ,写入aa.txt中,再继续编码回Unicode,写入aw.txt中*/ 
  75.      /*如果不存在a.txt文件,则程序出错,没有做错误处理*/ 
  76.      char* filename = "a.txt"
  77.      char* filenamea = "aa.txt"
  78.      char* filenamew = "aw.txt"
  79.      FILE*     input=fopen( filename, "rb"); 
  80.      FILE*     inputa=fopen( filenamea, "wb"); 
  81.      FILE*     inputw=fopen( filenamew, "wb"); 
  82.      wchar_t * buf ; 
  83.      /*BOE设置,UTF-16LE的BOE为FEFF,如果不先将其读取出来,wcstombs会调用失败*/ 
  84.      fgetwc(input); 
  85.      fputwc(0xFEFF,inputw); 
  86.      /*开始读取文件*/ 
  87.      while(!feof(input)) 
  88.      { 
  89.         buf = (wchar_t *)malloc(sizeof(wchar_t)*BUFF_SIZE)         ; 
  90.         memset(buf,    0, sizeof(wchar_t) * BUFF_SIZE ); 
  91.         fgetws(buf,    BUFF_SIZE,    input); 
  92.         fputs(w2m(buf),    inputa); 
  93.         fputws(m2w(w2m(buf)),    inputw); 
  94.      } 
  95.      /*后续处理*/ 
  96.      fclose(input); 
  97.      fclose(inputa); 
  98.      fclose(inputw); 
  99.      free(buf); 
  100.  
  101.      return 0; 

void ConvertGBKToUtf8(CString& strGBK) {
    int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
    unsigned short * wszUtf8 = new unsigned short[len+1];
    memset(wszUtf8, 0, len * 2 + 2);
    MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);

    len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL); 
    char *szUtf8=new char[len + 1];
    memset(szUtf8, 0, len + 1);
    WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);

    strGBK = szUtf8;
    delete[] szUtf8;
    delete[] wszUtf8;
}

void ConvertUtf8ToGBK(CString& strUtf8) {
    int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
    unsigned short * wszGBK = new unsigned short[len+1];
    memset(wszGBK, 0, len * 2 + 2);
    MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);

    len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); 
    char *szGBK=new char[len + 1];
    memset(szGBK, 0, len + 1);
    WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);

    strUtf8 = szGBK;
    delete[] szGBK;
    delete[] wszGBK;
}

posted @ 2012-08-16 10:28  _iCDev_Zhou  Views(192)  Comments(0Edit  收藏  举报