C/C++ 編碼轉換


(1) iconv_t iconv_open(const char *tocode, const char *fromcode);
//此函數說明將要進行哪兩種編碼的轉換,tocode是目標編碼,fromcode是原編碼,該函數返回一個轉換句柄,供以下兩個函數使用。
(2) size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);
//此函數從inbuf中讀取字符,轉換後輸出到outbuf中,inbytesleft用以記錄還未轉換的字符數,outbytesleft用以記錄輸出緩衝的剩餘空間。
(3) int iconv_close(iconv_t cd);
//此函數用於關閉轉換句柄,釋放資源。

C++ Linux實現

#ifndef WIN32
#include <iconv.h>
#define OUTLEN 500
class CodeConverter {

private:
	iconv_t cd;
public:

	// 構造
	CodeConverter(const char *from_charset, const char *to_charset) {
		cd = iconv_open(to_charset, from_charset);
	}


	// 析構
	~CodeConverter() {
		iconv_close(cd);
	}


	// 轉換輸出
	int convert(char *inbuf, int inlen, char *outbuf, int outlen) {
		char **pin = &inbuf;
		char **pout = &outbuf;

		//memset(outbuf, 0, outlen);
		return iconv(cd, pin, (size_t *)&inlen, pout, (size_t *)&outlen);
	}
};

C Linux實現

char *in_utf8 = "姝e?ㄥ??瑁?";
char *in_gb2312 = "正在安裝";
char out[OUTLEN];
 
/*unicode碼轉爲gb2312碼*/
rc = u2g(in_utf8,strlen(in_utf8),out,OUTLEN);
printf("unicode-->gb2312 out=%sn",out);
//gb2312碼轉爲unicode碼
 
rc = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN);
printf("gb2312-->unicode out=%sn",out);
}
/*代碼轉換:從一種編碼轉爲另一種編碼*/
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
iconv_t cd;
int rc;
char **pin = &inbuf;
char **pout = &outbuf;
 
cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
/*UNICODE碼轉爲GB2312碼*/
int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
/*GB2312碼轉爲UNICODE碼*/
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}


轉換接口

string UTF8ToANSI(const string str)
	{
#ifdef WIN32
		return UnicodeToANSI(UTF8ToUnicode(str));
#else
		static char out[OUTLEN] = { 0 };
		memset(out, 0, OUTLEN);
		// utf-8-->gb2312
		char* pTmp = (char*)str.c_str();
		CodeConverter cc = CodeConverter("utf-8", "gb2312");
		cc.convert(pTmp,strlen(pTmp), out, OUTLEN);
		::string strtmp(out, strlen(out));
		return strtmp;
#endif
	}

	string ANSIToUTF8(const string str)
	{
#ifdef WIN32
		return UnicodeToUTF8(ANSIToUnicode(str));
#else
		static char out[OUTLEN];
		memset(out, 0, OUTLEN);
		CodeConverter cc2 = CodeConverter("gb2312", "utf-8");
		cc2.convert((char*)str.c_str(), str.length(), out, OUTLEN);
		::string strtmp (out, strlen(out));
		return strtmp;
#endif
	}

windows實現

ANSI TO UFT8

string UnicodeToUTF8(const wstring& str)
	{
#ifdef WIN32
		char*     pElementText;
		int    iTextLen;
		iTextLen = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
		pElementText = new char[iTextLen + 1];
		memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
		::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
		string strText;
		strText = pElementText;
		delete[] pElementText;
		return strText;
#else
		return "";
#endif
	}
	wstring ANSIToUnicode(const string& str)
	{
#ifdef WIN32
		int  len = 0;
		len = str.length();
		int  unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
		wchar_t *  pUnicode;
		pUnicode = new  wchar_t[unicodeLen + 1];
		memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
		::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
		wstring  rt;
		rt = (wchar_t*)pUnicode;
		delete  pUnicode;
		return  rt;
#else
		wstring  rt;
		return rt;


UTF8 TO ANSI

wstring UTF8ToUnicode(const string& str)
	{
#ifdef WIN32
		int  len = 0;
		len = str.length();
		int  unicodeLen = ::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);
		wchar_t *  pUnicode;
		pUnicode = new  wchar_t[unicodeLen + 1];
		memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
		::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen);
		wstring  rt;
		rt = (wchar_t*)pUnicode;
		delete  pUnicode;
		return  rt;
#else
		wstring  rt;
		return rt;
#endif	
	}

	string UnicodeToANSI(const wstring& str)
	{
#ifdef WIN32
		char*     pElementText;
		int    iTextLen;
		// wide char to multi char
		iTextLen = WideCharToMultiByte(CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL);
		pElementText = new char[iTextLen + 1];
		memset((void*)pElementText, 0, sizeof(char) * (iTextLen + 1));
		::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
		string strText;
		strText = pElementText;
		delete[] pElementText;
		return strText;







發佈了49 篇原創文章 · 獲贊 8 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章