提莫打野出装:[C语言]字符串处理 - ANSI - Unicode - UTF8 转换

来源:百度文库 编辑:中财网 时间:2024/05/11 02:25:35

[C语言]字符串处理 - ANSI - Unicode - UTF8 转换

时间:2010-08-17 09:21来源:百度博客 作者:zj41342626 点击:1187次[C语言]字符串处理 - ANSI - Unicode - UTF8 转换 2008-11-4: 使用MultiByteToWideChar和WideCharToMultiByte写的4个ANSI - Unicode - UTF-8 相互转换的函数。 2008-11-5: 使用C语言标准库mbstowcs和wcstombs写的w2m和m2w两个函数,分别对应ANSI - Unicode 相互转换。收藏到:           
TAG: C语言  字串处理  unicode  UTF8  ANSI  

2008-11-4: 使用MultiByteToWideChar和WideCharToMultiByte写的4个ANSI <-> Unicode <-> UTF-8 相互转换的函数。
2008-11-5: 使用C语言标准库mbstowcs和wcstombs写的w2m和m2w两个函数,分别对应ANSI <-> Unicode 相互转换。
2008-11-8: 包装一下 ANSI <-> UTF-8 。

  1. #include  
  2. #include  
  3. #include  
  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;