一、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;
}