码迷,mamicode.com
首页 > Windows程序 > 详细

Windows下编码转换相关

时间:2014-09-10 23:45:31      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:os   ar   文件   art   on   c   line   ad   new   

一、ANSI转换为UTF-16

#include <windows.h>

wchar_t* AnsiToUnicodeBegin(const char* str)
{
  char* sz = new char[strlen(str)+1];
  strcpy(sz, str);
  int wLen = MultiByteToWideChar(CP_ACP, 0, sz, -1, NULL, 0);
  wchar_t* wBuf = new wchar_t[wLen+1];
  MultiByteToWideChar(CP_ACP, 0, sz, -1, wBuf, wLen);
  delete[] sz;
  return wBuf;
}

void AnsiToUnicodeEnd(wchar_t* wstr_chaged)
{
  if(wstr_chaged)
  {
    delete[] wstr_chaged;
    wstr_chaged = NULL;
  }
}

二、UTF-8转换为UTF-16

wchar_t* UTF8ToUnicodeBegin(const char* str)
{
  char* sz = new char[strlen(str)+1];
  strcpy(sz, str);
  int wLen = MultiByteToWideChar(CP_UTF8, 0, sz, -1, NULL, 0);
  wchar_t* wBuf = new wchar_t[wLen+1];
  MultiByteToWideChar(CP_UTF8, 0, sz, -1, wBuf, wLen);
  delete[] sz;
  return wBuf;
}

void UTF8ToUnicodeEnd(wchar_t* wstr_chaged)
{
  if(wstr_chaged)
  {
    delete[] wstr_chaged;
    wstr_chaged = NULL;
  }
}

三、UTF-16转换为UTF-8

char* UnicodeToUTF8Begin(const wchar_t* wstr)
{
  wchar_t *sz = new wchar_t[wcslen(wstr)+1];
  wcscpy(sz,wstr);
  int len = WideCharToMultiByte(CP_UTF8,0,sz,-1,NULL,0,0,0);
  char* buf = new char[len+1];
  WideCharToMultiByte(CP_UTF8,0,sz,-1,buf,len,NULL,NULL);
  delete[] sz;
  return buf;
}

void UnicodeToUTF8End(const char* str_chaged)
{
  if(str_chaged)
  {
    delete[] str_chaged;
    str_chaged = NULL;
  }
}

四、读写UTF-8格式文件,转换为UTF-16处理,最后转回UTF-8写入

int main()
{
  char lineBuf[256]={0};
  char headFlag[3];

  FILE *fpr;
  fpr = fopen("c:\\test.txt","rb");
  fread(headFlag,1,3,fpr);
  fgets(lineBuf,256,fpr);
  wchar_t* wstr = UTF8ToUnicodeBegin(lineBuf);

  char* str = UnicodeToUTF8Begin(wstr);

  FILE *fpw;
  fpw = fopen("c:\\outputUTF-8.txt","w");
  fwrite(headFlag,1,3,fpw);
  fwrite(str,1,strlen(str),fpw);


  UnicodeToUTF8End(str);
  UTF8ToUnicodeEnd(wstr);

  fclose(fpr);
  fclose(fpw);

  system("pause");
  return 0;
}

Windows下编码转换相关

标签:os   ar   文件   art   on   c   line   ad   new   

原文地址:http://www.cnblogs.com/elitiwin/p/3965308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!