GBK和UTF8之間的轉換能夠使用MultiByteToWideChar和WideCharToMultiByte兩個API,方法是先把它們轉換爲中間編碼Unicode,再轉換爲對應的編碼便可。windows
#include <stdio.h>網絡
#include <windows.h>ide
//GBK編碼轉換到UTF8編碼函數
int GBKToUTF8(unsigned char * lpGBKStr,unsigned char * lpUTF8Str,int nUTF8StrLen)編碼
{code
wchar_t * lpUnicodeStr = NULL;字符串
int nRetLen = 0;get
if(!lpGBKStr) //若是GBK字符串爲NULL則出錯退出it
return 0;io
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,NULL,NULL); //獲取轉換到Unicode編碼後所須要的字符空間長度
lpUnicodeStr = new WCHAR[nRetLen + 1]; //爲Unicode字符串空間
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,lpUnicodeStr,nRetLen); //轉換到Unicode編碼
if(!nRetLen) //轉換失敗則出錯退出
return 0;
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,NULL); //獲取轉換到UTF8編碼後所須要的字符空間長度
if(!lpUTF8Str) //輸出緩衝區爲空則返回轉換後須要的空間大小
{
if(lpUnicodeStr)
delete []lpUnicodeStr;
return nRetLen;
}
if(nUTF8StrLen < nRetLen) //若是輸出緩衝區長度不夠則退出
{
if(lpUnicodeStr)
delete []lpUnicodeStr;
return 0;
}
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)lpUTF8Str,nUTF8StrLen,NULL,NULL); //轉換到UTF8編碼
if(lpUnicodeStr)
delete []lpUnicodeStr;
return nRetLen;
}
// UTF8編碼轉換到GBK編碼
int UTF8ToGBK(unsigned char * lpUTF8Str,unsigned char * lpGBKStr,int nGBKStrLen)
{
wchar_t * lpUnicodeStr = NULL;
int nRetLen = 0;
if(!lpUTF8Str) //若是UTF8字符串爲NULL則出錯退出
return 0;
nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,NULL,NULL); //獲取轉換到Unicode編碼後所須要的字符空間長度
lpUnicodeStr = new WCHAR[nRetLen + 1]; //爲Unicode字符串空間
nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,lpUnicodeStr,nRetLen); //轉換到Unicode編碼
if(!nRetLen) //轉換失敗則出錯退出
return 0;
nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,NULL,NULL,NULL,NULL); //獲取轉換到GBK編碼後所須要的字符空間長度
if(!lpGBKStr) //輸出緩衝區爲空則返回轉換後須要的空間大小
{
if(lpUnicodeStr)
delete []lpUnicodeStr;
return nRetLen;
}
if(nGBKStrLen < nRetLen) //若是輸出緩衝區長度不夠則退出
{
if(lpUnicodeStr)
delete []lpUnicodeStr;
return 0;
}
nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,(char *)lpGBKStr,nRetLen,NULL,NULL); //轉換到GBK編碼
if(lpUnicodeStr)
delete []lpUnicodeStr;
return nRetLen;
}
//使用這兩個函數的例子
int main()
{
char cGBKStr[] = "我是中國人!";
char * lpGBKStr = NULL;
char * lpUTF8Str = NULL;
FILE * fp = NULL;
int nRetLen = 0;
nRetLen = GBKToUTF8((unsigned char *)cGBKStr,NULL,NULL);
printf("轉換後的字符串須要的空間長度爲:%d ",nRetLen);
lpUTF8Str = new char[nRetLen + 1];
nRetLen = GBKToUTF8((unsigned char *)cGBKStr,(unsigned char *)lpUTF8Str,nRetLen);
if(nRetLen)
{
printf("GBKToUTF8轉換成功!");
}
else
{
printf("GBKToUTF8轉換失敗!");
goto Ret0;
}
fp = fopen("C:\GBK轉UTF8.txt","wb"); //保存到文本文件
fwrite(lpUTF8Str,nRetLen,1,fp);
fclose(fp);
getchar(); //先去打開那個文本文件看看,單擊記事本的「文件」-「另存爲」菜單,在對話框中看到編碼框變爲了「UTF-8」說明轉換成功了
nRetLen = UTF8ToGBK((unsigned char *)lpUTF8Str,NULL,NULL); //再轉回來
printf("轉換後的字符串須要的空間長度爲:%d ",nRetLen);
lpGBKStr = new char[nRetLen + 1];
nRetLen = UTF8ToGBK((unsigned char *)lpUTF8Str,(unsigned char *)lpGBKStr,nRetLen);
if(nRetLen)
{
printf("UTF8ToGBK轉換成功! ");
}
else
{
printf("UTF8ToGBK轉換失敗! ");
goto Ret0;
}
fp = fopen("C:\UTF8轉GBK.txt","wb"); //保存到文本文件
fwrite(lpGBKStr,nRetLen,1,fp);
fclose(fp);
getchar(); //再去打開文本文件看看,發現編碼框又變爲了「ANSI」說明轉換成功了
Ret0:
if(lpGBKStr)
delete []lpGBKStr;
if(lpUTF8Str)
delete []lpUTF8Str;
return 0;
}