c/c++ GB2312编码和UTF-8互转

时间:2023-01-05 23:59:09
#include <stdio.h>
#include <Windows.h>
#include <string.h>
 
/*************************************************
*将GB2312编码的字符串转为UTF-8编码
*输入:
*p:指向待转码字符串
*返回:
*指向已转码字符串的指针
*过程:
*将GB2312转为Unicode编码
*再将Unicode转为UTF-8
*************************************************/
char* Gb2312ToUtf8(char *p){
	DWORD dwNum = MultiByteToWideChar(CP_ACP, 0, p, -1, NULL, 0);
	char *psText;
	wchar_t *pwText = (wchar_t*)malloc(dwNum*sizeof(wchar_t));
	dwNum = MultiByteToWideChar(CP_ACP ,0 ,p ,-1 ,pwText ,dwNum );
	dwNum = WideCharToMultiByte(CP_UTF8,0,pwText,-1,NULL,0,NULL,NULL);
	psText=(char*)malloc(dwNum*sizeof(char));
	dwNum = WideCharToMultiByte(CP_UTF8,0,pwText,-1,psText,dwNum,NULL,NULL);
	free(pwText);
	return psText;
}
 
/*************************************************
*将UTF-8编码的字符串转为GB2312编码
*输入:
*p:指向待转码字符串
*返回:
*指向已转码字符串的指针
*过程:
*将UTF-8转为Unicode编码
*再将Unicode转为GB2312
*************************************************/
char* Utf8ToGb2312(char *p){
	DWORD dwNum = MultiByteToWideChar(CP_UTF8,0,p,-1,NULL,0);
	char *psText;
	wchar_t *pwText=(wchar_t*)malloc(dwNum*sizeof(wchar_t));
	dwNum = MultiByteToWideChar(CP_UTF8,0,p,-1,pwText,dwNum);
	dwNum = WideCharToMultiByte(CP_ACP,0,pwText,-1,NULL,0,NULL,NULL);
	psText=(char*)malloc(dwNum*sizeof(char));
	dwNum = WideCharToMultiByte(CP_ACP,0,pwText,-1,psText,dwNum,NULL,NULL);
	free(pwText);
	return psText;
}
 
int main(){
	char *source = "Hello world!";                                       //英文字符串测试
	char *result;
	result = Gb2312ToUtf8(source);
	printf("%s\n",result);
	result = Utf8ToGb2312(result);
	printf("%s\n",result);
	source="测试";
	result = Gb2312ToUtf8(source);                                   //中文字符串测试,不支持UTF-8编码汉字,会出现乱码
	printf("%s\n",result);
	result = Utf8ToGb2312(result);									//GB2312编码的汉字则能正常显示
	printf("%s\n",result);
	system("pause");
	return 0;
}