Windows API GB2312/Unicode/UTF-8轉換

來源:互聯網
上載者:User

轉自:http://www.cppblog.com/fdsajhg/archive/2010/09/03/125770.aspx

BYTE to Unicode

//////////////BYTE dwByte[200];wchar_t dwcharw[200];for(int i=0;i<200;i++){dwcharw[i]=dwByte[i];}

(char--for--unicode)

Windows API GB2312/Unicode/UTF-8轉換

/* * GB2312/Unicode/UTF-8轉換 * WLCIMS WorkStation 2008-09-08 * http://wlcims.cn */ #ifndef __OCUGX_H__ #define __OCUGX_H__ class ocUgx{ public: // --------- UTF-8轉Unicode,單字 ---------------- int cU8xU(WCHAR* pOut,char *pText){ int ret = 0; char* uchar = (char *)pOut; unsigned cIn = (unsigned char)pText[0]; if(cIn<0x80){ // ASCII 0x00 ~ 0x7f pOut[0] = pText[0]; }else if(cIn<0xdf){ uchar[0] = (pText[0]<<6)|(pText[1]&0x3f); uchar[1] = (pText[0]>>2)&0x0f; ret = 1; }else if(cIn<0xef){ uchar[0] = (pText[1]<<6)|(pText[2]&0x3f); uchar[1] = (pText[0]<<4)|((pText[1]>>2)&0x0f); ret = 2; }else if(cIn<0xf7){ uchar[0] = (pText[2]<<6)|(pText[3]&0x3f); uchar[1] = (pText[1]<<4)|((pText[2]>>2)&0x0f); uchar[2] = ((pText[0]<<2)&0x1c)|((pText[1]>>4)&0x03); ret = 3; } return ret; } // ---------- Unicode轉UTF-8,單字 ------------------ int cUxU8(char* pOut,WCHAR* pText){ int ret = 0; unsigned char* pchar = (unsigned char *)pText; if(pText[0]<=0x7f){ // ASCII 0x00 ~ 0x7f pOut[0] = (char)pchar[0]; }else if(pText[0]<=0x7ff){ // 0x080 ~ 0x7ff pOut[0] = 0xc0|(pchar[1]<<2)|(pchar[0]>>6); pOut[1] = 0x80|(pchar[0]&0x3f); ret = 1; }else{ // 0x0800 ~ 0xFFFF pOut[0] = 0xe0|(pchar[1]>>4); pOut[1] = 0x80|((pchar[1]&0x0f)<<2)|(pchar[0]>>6); pOut[2] = 0x80|(pchar[0]&0x3f); ret = 2; } return ret; } // ----------- Unicode轉GB2312,單字 ------------------ int cUxG(char* pOut,WCHAR* pText){ int ret = 0; if(pText[0]<0x80){ // ASCII 0x00 ~ 0x7f pOut[0] = (char)pText[0]; }else{ ::WideCharToMultiByte(CP_ACP,0,pText,1,pOut,sizeof(WCHAR),NULL,NULL); ret = 1; } return ret; } // ----------- GB2312轉Unicode,單字 ------------------ int cGxU(WCHAR* pOut,char* pText){ int ret = 0; if((unsigned)pText[0]<0x80){ // ASCII 0x00 ~ 0x7f pOut[0] = (WCHAR)pText[0]; }else{ ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,pText,2,pOut,1); ret = 1; } return ret; } // ------------ UTF-8轉Unicode,字串 ---------------- int sU8xU(WCHAR* pOut,char *pText,int Len){ int i,j; for(i=0,j=0;i<Len;i++,j++){ i+=cU8xU(&pOut[j],&pText[i]); } return j; } // ------------ Unicode轉UTF-8,字串 ---------------- int sUxU8(char* pOut,WCHAR* pText,int Len){ int i,j; for(i=0,j=0;i<Len;i++,j++){ j+=cUxU8(&pOut[j],&pText[i]); } return j; } // ------------ Unicode轉GB2312,字串 ---------------- int sUxG(char* pOut,WCHAR* pText,int Len){ int i,j; for(i=0,j=0;i<Len;i++,j++){ j+=cUxG(&pOut[j],&pText[i]); } return j; } // ------------ GB2312轉Unicode,字串 ---------------- int sGxU(WCHAR* pOut,char* pText,int Len){ int i,j; for(i=0,j=0;i<Len;i++,j++){ i+=cGxU(&pOut[j],&pText[i]); } return j; } // ------------ GB2312轉UTF-8,字串 ------------------ int sGxU8(char* pOut,char* pText,int Len){ int i,j; WCHAR buf; for(i=0,j=0;i<Len;i++,j++){ if((unsigned)pText[0]<0x80){ // ASCII 0x00 ~ 0x7f pOut[j] = pText[i]; }else{ i += cGxU(&buf,&pText[i]); j += cUxU8(&pOut[j],&buf); } } return j; } // ------------ UTF-8轉GB2312,字串 ------------------ int sU8xG(char* pOut,char* pText,int Len){ int i,j; WCHAR buf; for(i=0,j=0;i<Len;i++,j++){ if((unsigned)pText[0]<0x80){ // ASCII 0x00 ~ 0x7f pOut[j] = pText[i]; }else{ i += cU8xU(&buf,&pText[i]); j += cUxG(&pOut[j],&buf); } } return j; } }; #endif 

感謝你的貼子,協助我解決了困擾一下午的難題,不過發現其中有出筆誤。下面代碼裡if條件判斷運算式中的pText[0] 應該是pText[i]

// ------------ UTF-8轉GB2312,字串 ------------------
int sU8xG(char* pOut,char* pText,int Len){
int i,j;
WCHAR buf;
for(i=0,j=0;i<Len;i++,j++){
if((unsigned)pText[0]<0x80){ // ASCII 0x00 ~ 0x7f
pOut[j] = pText[i];
}else{
i += cU8xU(&buf,&pText[i]);
j += cUxG(&pOut[j],&buf);
}
}
return j;
}
Feedback
# re: Windows API GB2312/Unicode/UTF-8轉換 回複 更多評論
2011-02-22 02:06 by Michael Genn
我也來糾正個錯誤: cU8xU (UTF-8轉Unicode, 單字) 這個函數在轉換全形的逗號(",")時出現轉換不正確的問題.

原因在於判斷cIn值的部分有問題, 應該把其中的一些小於符號改為小於等於.
下面是我給出的改正版:

int cU8xU(wchar_t* pOut,char *pText)
{
int ret = 0;
char* uchar = (char *)pOut;
unsigned cIn = (unsigned char)pText[0];
if(cIn<0x80){ // ASCII 0x00 ~ 0x7f
pOut[0] = pText[0];
}else if(cIn<0xdf){
uchar[0] = (pText[0]<<6)|(pText[1]&0x3f);
uchar[1] = (pText[0]>>2)&0x0f;
ret = 1;
}else if(cIn<=0xef){
uchar[0] = (pText[1]<<6)|(pText[2]&0x3f);
uchar[1] = (pText[0]<<4)|((pText[1]>>2)&0x0f);
ret = 2;
}else if(cIn<0xf7){
uchar[0] = (pText[2]<<6)|(pText[3]&0x3f);
uchar[1] = (pText[1]<<4)|((pText[2]>>2)&0x0f);
uchar[2] = ((pText[0]<<2)&0x1c)|((pText[1]>>4)&0x03);
ret = 3;
}
return ret;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.