UTF8转GB2312

1.UTF-8先到UNICODE

int UTF82UCS(unsigned char *pUTF8, wchar_t *pUCS)
{
	int UCSlen = 0, i;
	unsigned char *pTempUCS = NULL;
	unsigned char *pTempUTF8 = NULL;

	if(pUCS == NULL || pUTF8 == NULL)
		return -1;
	UCSlen = MultiByteToWideChar(CP_UTF8,0,(LPCSTR)pUTF8,-1,NULL,0);
	UCSlen--;
	pTempUCS = (unsigned char*)pUCS;
	pTempUTF8 = pUTF8;
	for(i = 0; i < UCSlen; i++)
	{
		if((*pTempUTF8) <= 0x7F)  //1 byte
		{
			*(pTempUCS + 1) = 0x00;
			*pTempUCS = *(pTempUTF8++);
			pTempUCS += 2;
		}
		else if((*pTempUTF8) >= 0xC0 && (*pTempUTF8) <= 0xDF)//2 bytes
		{
			*(pTempUCS + 1) = ((*pTempUTF8) >> 2) & 0x07;
			*pTempUCS = ((*pTempUTF8) << 6) | (*(pTempUTF8 + 1) & 0x3F);
			pTempUTF8 += 2;
			pTempUCS += 2;
		}
		else//3 bytes
		{
			*(pTempUCS + 1) = ((*pTempUTF8) << 4) | ((*(pTempUTF8 + 1) >> 2) & 0x0F);
			pTempUTF8++;
			*pTempUCS = ((*pTempUTF8) << 6) | (*(pTempUTF8+1) & 0x3F);
			pTempUTF8 += 2;
			pTempUCS += 2;
		}
	}
	return UCSlen;
}

2.UNICODE到GB2312

void UnicodeToGB2312(char* pOut,wchar_t* uData,int nLen)
{
	WideCharToMultiByte(CP_ACP,NULL,uData,nLen,pOut,sizeof(wchar_t)*nLen,NULL,NULL);
	return;
}      



完整的例子

wchar_t pUnicode[128]= {0};
				char pGB2312[256]= {0};
				int nUnicodeLen = UTF82UCS((unsigned char *)pUtf8,(wchar_t*)pUnicode);

				UnicodeToGB2312((char*)&pGB2312,(wchar_t*)&pUnicode,nUnicodeLen);

				


你可能感兴趣的:(null,byte)