http://www.vckbase.com/document/viewdoc/?id=1397
相信一定有不少的程序開發人員時常會遇到字符編碼的問題,而這個問題也是非常讓人頭痛的。因為這些都是潛在的錯誤,要找出這些錯誤也得要有這方面的開發經驗才行。特別是在處理xml文檔時 ,該問題的出現就更加的頻繁了,有一次用java寫服務器端程序,用vc寫客戶端與之交互。交互的協議都是用xml寫的。結果在通訊時老是發現數據接受不正確。納悶!於是用抓取網絡數據包工具抓取數據,后來才發現原來是java上xml的頭是這樣的<?xml version="1.0" encoding="UTF-8"?>,而vc上默認的是GB2312。所以一遇到漢字數據就不正確了。去網上找資料,這方面的文章好象特別少,針對像這樣的問題,下面我介紹一下我自己寫的一個轉換程序。當然,程序很簡單。如果有畫蛇添足的地方,還望各位高手一笑了之。
如果您對UTF-8、Unicode、GB2312等還是很陌生的話,請查看http://www.linuxforum.net/books/UTF-8-Unicode.html,我這里就不浪費口舌了。下面介紹一下WinAPI的兩個函數:WideCharToMultiByte、MultiByteToWideChar。
函數原型:
int WideCharToMultiByte( UINT CodePage, // code page DWORD dwFlags, // performance and mapping flags LPCWSTR lpWideCharStr, // wide-character string int cchWideChar, // number of chars in string LPSTR lpMultiByteStr, // buffer for new string int cbMultiByte, // size of buffer LPCSTR lpDefaultChar, // default for unmappable chars LPBOOL lpUsedDefaultChar // set when default char used ); //將寬字符轉換成多個窄字符 int MultiByteToWideChar( UINT CodePage, // code page DWORD dwFlags, // character-type options LPCSTR lpMultiByteStr, // string to map int cbMultiByte, // number of bytes in string LPWSTR lpWideCharStr, // wide-character buffer int cchWideChar // size of buffer );//將多個窄字符轉換成寬字符
需要用到的一些函數:
CString CXmlProcess::HexToBin(CString string)//將16進制數轉換成2進制 { if( string == "0") return "0000"; if( string == "1") return "0001"; if( string == "2") return "0010"; if( string == "3") return "0011"; if( string == "4") return "0100"; if( string == "5") return "0101"; if( string == "6") return "0110"; if( string == "7") return "0111"; if( string == "8") return "1000"; if( string == "9") return "1001"; if( string == "a") return "1010"; if( string == "b") return "1011"; if( string == "c") return "1100"; if( string == "d") return "1101"; if( string == "e") return "1110"; if( string == "f") return "1111"; return ""; } CString CXmlProcess::BinToHex(CString BinString)//將2進制數轉換成16進制 { if( BinString == "0000") return "0"; if( BinString == "0001") return "1"; if( BinString == "0010") return "2"; if( BinString == "0011") return "3"; if( BinString == "0100") return "4"; if( BinString == "0101") return "5"; if( BinString == "0110") return "6"; if( BinString == "0111") return "7"; if( BinString == "1000") return "8"; if( BinString == "1001") return "9"; if( BinString == "1010") return "a"; if( BinString == "1011") return "b"; if( BinString == "1100") return "c"; if( BinString == "1101") return "d"; if( BinString == "1110") return "e"; if( BinString == "1111") return "f"; return ""; } int CXmlProcess::BinToInt(CString string)//2進制字符數據轉換成10進制整型 { int len =0; int tempInt = 0; int strInt = 0; for(int i =0 ;i < string.GetLength() ;i ++) { tempInt = 1; strInt = (int)string.GetAt(i)-48; for(int k =0 ;k < 7-i ; k++) { tempInt = 2*tempInt; } len += tempInt*strInt; } return len; }
UTF-8轉換成GB2312先把UTF-8轉換成Unicode.然后再把Unicode通過函數WideCharToMultiByte轉換成GB2312
WCHAR* CXmlProcess::UTF_8ToUnicode(char *ustart) //把UTF-8轉換成Unicode { char char_one; char char_two; char char_three; int Hchar; int Lchar; char uchar[2]; WCHAR *unicode; CString string_one; CString string_two; CString string_three; CString combiString; char_one = *ustart; char_two = *(ustart+1); char_three = *(ustart+2); string_one.Format("%x",char_one); string_two.Format("%x",char_two); string_three.Format("%x",char_three); string_three = string_three.Right(2); string_two = string_two.Right(2); string_one = string_one.Right(2); string_three = HexToBin(string_three.Left(1))+HexToBin(string_three.Right(1)); string_two = HexToBin(string_two.Left(1))+HexToBin(string_two.Right(1)); string_one = HexToBin(string_one.Left(1))+HexToBin(string_one.Right(1)); combiString = string_one +string_two +string_three; combiString = combiString.Right(20); combiString.Delete(4,2); combiString.Delete(10,2); Hchar = BinToInt(combiString.Left(8)); Lchar = BinToInt(combiString.Right(8)); uchar[1] = (char)Hchar; uchar[0] = (char)Lchar; unicode = (WCHAR *)uchar; return unicode; } char * CXmlProcess::UnicodeToGB2312(unsigned short uData) //把Unicode 轉換成 GB2312 { char *buffer ; buffer = new char[sizeof(WCHAR)]; WideCharToMultiByte(CP_ACP,NULL,&uData,1,buffer,sizeof(WCHAR),NULL,NULL); return buffer; }
GB2312轉換成UTF-8:先把GB2312通過函數MultiByteToWideChar轉換成Unicode.然后再把Unicode通過拆開Unicode后拼裝成UTF-8。
WCHAR * CXmlProcess::Gb2312ToUnicode(char *gbBuffer) //GB2312 轉換成 Unicode { WCHAR *uniChar; uniChar = new WCHAR[1]; ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,uniChar,1); return uniChar; } char * CXmlProcess::UnicodeToUTF_8(WCHAR *UniChar) // Unicode 轉換成UTF-8 { char *buffer; CString strOne; CString strTwo; CString strThree; CString strFour; CString strAnd; buffer = new char[3]; int hInt,lInt; hInt = (int)((*UniChar)/256); lInt = (*UniChar)%256; CString string ; string.Format("%x",hInt); strTwo = HexToBin(string.Right(1)); string = string.Left(string.GetLength() - 1); strOne = HexToBin(string.Right(1)); string.Format("%x",lInt); strFour = HexToBin(string.Right(1)); string = string.Left(string.GetLength() -1); strThree = HexToBin(string.Right(1)); strAnd = strOne +strTwo + strThree + strFour; strAnd.Insert(0,"1110"); strAnd.Insert(8,"10"); strAnd.Insert(16,"10"); strOne = strAnd.Left(8); strAnd = strAnd.Right(16); strTwo = strAnd.Left(8); strThree = strAnd.Right(8); *buffer = (char)BinToInt(strOne); buffer[1] = (char)BinToInt(strTwo); buffer[2] = (char)BinToInt(strThree); return buffer; }
例子:將GB2312轉換成UTF-8的調用:
char * CXmlProcess::translateCharToUTF_8(char *xmlStream, int len) { int newCharLen =0 ; int oldCharLen = 0; int revCharLen = len; char* newCharBuffer; char* finalCharBuffer; char *buffer ; CString string; buffer = new char[sizeof(WCHAR)]; newCharBuffer = new char[int(1.5*revCharLen)];//設置最大的一個緩沖區 while(oldCharLen < revCharLen) { if( *(xmlStream + oldCharLen) >= 0) { *(newCharBuffer+newCharLen) = *(xmlStream +oldCharLen); newCharLen ++; oldCharLen ++; }//如果是英文直接復制就可以 else { WCHAR *pbuffer = this->Gb2312ToUnicode(xmlStream+oldCharLen); buffer = this->UnicodeToUTF_8(pbuffer); *(newCharBuffer+newCharLen) = *buffer; *(newCharBuffer +newCharLen +1) = *(buffer + 1); *(newCharBuffer +newCharLen +2) = *(buffer + 2); newCharLen += 3; oldCharLen += 2; } } newCharBuffer[newCharLen] = ''\0''; CString string1 ; string1.Format("%s",newCharBuffer); finalCharBuffer = new char[newCharLen+1]; memcpy(finalCharBuffer,newCharBuffer,newCharLen+1); return finalCharBuffer; }
/*
字符串編碼轉換 GBK to UTF8 (ansi版)
xmwen@126.com
*/
char *gbk2utf8(const char *strGBK){
int len;
wchar_t *strUnicode;
char *strUTF8;
if (!strGBK){return NULL;}
len = MultiByteToWideChar(CP_GBK, 0,strGBK, -1, NULL,0);
if (len <1){return NULL;}
strUnicode = (wchar_t *) malloc(sizeof(wchar_t) * len);
if (!strUnicode){return NULL;}
len = MultiByteToWideChar(CP_GBK, 0, strGBK, -1, strUnicode, len);
if (len<1){free(strUnicode);return NULL;}
len = WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, NULL, 0, NULL, NULL);
if (len<1){free(strUnicode);return NULL;}
strUTF8 = (char *) malloc(sizeof(char) * len);
if (!strUTF8){free(strUnicode);return NULL;}
len = WideCharToMultiByte (CP_UTF8, 0, strUnicode, -1, strUTF8, len, NULL,NULL);
free(strUnicode);
if (len<1){free(strUTF8);return NULL;}
return strUTF8;
} ( xmwen 發表於 2009-11-3 19:38:00)
[ 原創文檔 本文適合中級讀者 已閱讀34485次 ]
搞笑,這種害人害己的文章還有這么多人訪問。
作者光知道 WideCharToMultiByte 可以把 Unicode 轉成 GB2312 就不知道也可以把 Unicode 轉換為 UTF-8 嗎?
其實這是一個很簡單的程序,都被作者搞復雜了。
要實現 GB2312 (其實是GBK)轉換為 UTF-8 其實很簡單,先用 MultiByteToWideChar 把 GB2312 轉換為 Unicode,再用 WideCharToMultiByte 把 Unicode 轉換為 UTF-8 就可以了。
UTF-8 轉換為 GB2312 是個相反的過程,先用 MultiByteToWideChar 把 UTF-8 轉換為 Unicode,再用 WideCharToMultiByte 把 Unicode 轉換為 GB2312 就可以了。 ( 雁過留聲 發表於 2007-1-11 9:11:00)
translateCharToUTF_8的編碼不對,
請作者檢查一下,
如: "你是我的好朋友"
轉換成了;"浣犳槸鎴戠殑濂芥i脲弸鍚?"
正確的應是:
"浣犳槸鎴戠殑濂芥湅鍙嬪悧"
對於有的編碼還能對...
交流一下:kudoo.aos@gmail.com
( kudoo 發表於 2006-8-20 19:46:00)
shines在2005-2-6,提供了一段程序,里面有
buffersize = WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, unicode, wide_size, NULL, 0, NULL, 0);
buffer = new char[buffersize+1];
但是,我在調試的時候發現:buffersize似乎已經預先留了‘\0’的位置,或者是不是我出錯了
比如:“i love you,愛”GB2312是需要14個字節
UTF8是需要15個字節,返回時候就是這些了啊,
我的地址是:robin-fox@sohu.com,
誰能回答以下,感謝!! ( robin_fox_nan 發表於 2006-3-19 20:20:00)
暈.格式沒有了
原文請看
http://www.kbadboy.com/viewfull.asp?id=33 ( 鬼龍之舞 發表於 2005-8-25 16:13:00)
支持樓主!是因為你我才寫出來的,不管是在體積還是在速度,相信都比樓主的強一點,如果不考慮移植性的話
感謝樓主!!
UTF8toUnicode proc uses esi edi lpszBuf_OUT,lpszUTF8_IN
mov esi,lpszUTF8_IN
mov edi,lpszBuf_OUT
.while TRUE
mov al,[esi]
.if sbyte ptr al <0
mov al,[esi]
and al,00001111b
shl al,4
mov [edi+1],al
mov al,[esi+1]
and al,00111100b
shr al,2
or [edi+1],al
mov al,[esi+1]
and al,11b
shl al,6
mov [edi+0],al
mov al,[esi+2]
and al,00111111b
or [edi+0],al
add edi,2
add esi,3
.elseif al
xor ah,ah
stosw
inc esi
.else
mov WORD ptr [edi],0
.break
.endif
.endw
ret
UTF8toUnicode endp ( 鬼龍之舞 發表於 2005-8-25 16:11:00)
UnicodetoUTF8 proc uses esi edi lpBuf_OUT,lpszUTF8_IN
mov esi,lpszUTF8_IN
mov edi,lpBuf_OUT
.while TRUE
mov ax,[esi]
.if ax==0
stosw
.break
.elseif ah==0
add esi,2
stosw
.else
mov al,[esi+1]
shr al,4
or al,11100000b
mov [edi+0],al
mov al,[esi+1]
and al,00001111b
shl al,2
or al,10000000b
mov ah,[esi+0]
shr ah,6
or al,ah
mov [edi+1],al
mov al,[esi+0]
and al,00111111b
or al,10000000b
mov [edi+2],al
add edi,3
add esi,2
.endif
.endw
ret
UnicodetoUTF8 endp ( 鬼龍之舞 發表於 2005-8-25 16:11:00)
幫忙弄以下 ( zztop5384 發表於 2005-4-18 10:35:00)
int WideCharToMultiByte(
UINT CodePage, // code page
DWORD dwFlags, // performance and mapping flags
LPCWSTR lpWideCharStr, // wide-character string
int cchWideChar, // number of chars in string
LPSTR lpMultiByteStr, // buffer for new string
int cbMultiByte, // size of buffer
LPCSTR lpDefaultChar, // default for unmappable chars
LPBOOL lpUsedDefaultChar // set when default char used
); //將寬字符轉換成多個窄字符
這些只是函數原型,並沒有具體實現 ( zztop5384 發表於 2005-4-18 10:27:00)
//對不起,少加了個擴號
WCHAR* CXmlProcess::UTF_8ToUnicode(char *pText)
{
char uchar[2];
WCHAR *unicode;
char_one = pText[0];
char_two = pText[1]);
char_three = pText[2];
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
unicode = (WCHAR *)uchar;
return unicode;
}
( Mycro 發表於 2005-4-9 3:40:00)
zyl910:從Wndows2000開始,Windows系統就支持了UTF-8代碼頁65001
(甚至還支持UTF-7——65000)
先使用代碼頁65001調用MultiByteToWideChar將UTF-8轉為UTF-16LE(Windows中的Unicode字符串實際上是UTF-16LE)
再使用代碼頁936調用WideCharToMultiByte將UTF-16LE字符串轉為GB2312字符串
CP_ACP是指系統代碼頁
只有在“中文(中國)”的時候
CP_ACP才等價與GB2312
所以在必需轉成GB2312時必須寫清楚936
從Windows98開始
代碼頁936不再指GB2312-1980
而是GBK——GB2312擴展
GBK的收錄了2萬多字,簡體繁體都有(GB13000-1993的字匯范圍)
現在最新的漢子標准是GB18030-2000
允許4字節編碼的漢字
所以編碼空間是260萬
足夠收錄所有漢字
但是只有WindowsXP(及以上,如Windows Server 2003)才在內核上支持GB18030-2000(Win2000需要打補丁)
GB18030-2000的代碼頁是54936
(發表於2005-2-3 9:31:00) wuhuaqiang:什么都別說了,換一份工作吧;寫文章的事情不着急。
看過你的另一篇ftp斷點續傳的文章,學到了一些東西。
(發表於2005-2-3 10:20:00) sllihui:請作者貼出UTF-8轉換成GB2312的實現函數。謝謝。
我截獲網頁數據包。可以通過這種辦法將亂碼轉成漢字嗎??
(發表於2005-2-3 13:24:00) shines:
char *buffer = NULL;
int buffersize;
buffersize = WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, unicode, wide_size, NULL, 0, NULL, 0);
buffer = new char[buffersize+1];
BOOL bUsedDefaultChar = FALSE;
int sizeUTF = WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, unicode, wide_size,
buffer, buffersize+1,
NULL, NULL);
if (sizeUTF != 0)
{
buffer[sizeUTF] = 0;
strUTF = buffer;
bSuccess = true;
}
else
{
strUTF = _T("");
bSuccess = false;
}
delete[] unicode;
delete[] buffer;
delete[] byteArray;
return bSuccess;
}
(發表於2005-2-6 17:19:00) shines:bool ConvGB2312ToUTF8(CString strGB2312, CString& strUTF)
{
int nLength = strGB2312.GetLength();
bool bSuccess = false;
if (nLength == 0)
{
strUTF = _T("");
return false;
}
int nByteLength = nLength + 1;
char *byteArray = NULL;
byteArray = new char[nByteLength];
if (byteArray == NULL)
return false;
strcpy(byteArray, (LPCTSTR)strGB2312);
wchar_t *unicode = NULL;
int uni_size;
uni_size = MultiByteToWideChar(LANG_CP_GB2312, 0, byteArray, nLength, NULL, 0);
unicode = new wchar_t[uni_size+1];
int wide_size = MultiByteToWideChar(LANG_CP_GB2312, 0, byteArray, nLength, unicode, uni_size+1);
unicode[wide_size] = 0;
if (wide_size == 0)
{
delete[] buffer;
return false;
}
(發表於2005-2-6 17:19:00) shines:UTF-8就是專門手機這種嵌入式設備的新的編碼法,他的特點是,傳統的ASCII字符還是以一個字
節來表示的,但是如果字符不屬於ASCII字符集時,就用兩至三個位來表示。
在 0x0001-0x007F之間的字符(傳統的ASCII字符)用一個位來表示
0 | bits0-6
在 0x000以及在0x0080-0x07FF之間的字符使用下面來表示:
1 | 1 | 0 | bits 6-10 | 1 | 0 | bits 0-5
如果虛擬機看到這樣的一個字符的話,虛擬機會把第一個字節最前頭的110以及第二個字節的前頭
的10拿掉把剩下的位重新組合成一個2字節的數位來表示字符:
00000 | bits 6-10 | bits 0-5
同理,0x0800 - 0xFFFF的字符表示:
1 | 1 | 1 | 0 | bits 12-15 | 1 | 0 | bits 6-11 | 1 | 0 | bits 0-5
也可以用同樣的方法重新組合成一個兩個字節的字符串來
特別需要注意的是kjava中的null字符也使用兩個字節來表示而不是一個字節:)
******************************************************************************/
(發表於2005-2-6 17:21:00) shines:/*****************************************************************************
手機里面的字符串基本上都是采用的UTF-8的編碼法。
而我們在PC機器上所采用的基本上都是ASCII和unicode編碼法
ASCII編碼法是單字節的編碼方法,只能表示256個字符,英文字母是足夠了
但是無法表示漢字
unicode是雙字節的編碼法,可以用來表示漢字,但是卻對於一般的英文字母浪費了太多的空間
(至少面對於手機的存儲是這樣的)。
(發表於2005-2-6 17:21:00) shines:貼一個我自己寫的GB2312轉UTF8的函數,也許比樓主的簡單一些,如果是要轉換成的格式那種的話,則需要另外寫,其實中的 "FFFF" 就是Unicode的十六進制表示。
這里的UTF8編碼方式,與Java的InputStream.WriteUTF()的編碼方式一致。
由於評論只能貼1000個字,所以采取這種倒貼的方式,請見諒,大家合起來看吧
(發表於2005-2-6 17:23:00) shines:另外補充一下,我的程序也許只能在Win2000、WinNT以后(XP,Win2003等)的系統工作,Win98下是否可以沒有試過,這年頭很少用這個系統了
(發表於2005-2-6 17:27:00) Mycro:誰知道Unix下,除了ICU,還有什么更簡單的辦法?
我不想用XML4C來搞。。
(發表於2005-2-16 21:48:00) hryyx:強烈建議作者貼出UTF-8轉換成GB2312的實現函數。謝謝。
否則文章與題目就不符了,UTF-8與GB2312之間的“互換”
(發表於2005-2-18 10:48:00) CodeStylite:// LPSTR str = "浠嬬粛";
LPSTR str = new char[14];
BYTE a[] = { 0x54, 0x65, 0x78, 0x74, 0xE5,
0x92, 0x8C, 0xE4, 0xB8, 0xAD,
0xE6, 0x96, 0x87 };
memset( str, 0, 14 );
memcpy( str, a, 13 );
CString strGB;
/*可逆
// int n = (str[0]+256)*256+str[1]+256;
// char c1,c2;
// c1 = n/256 - 256;
// c2 = n%256 - 256;
// strGB.Format("%c%c", c1, c2 );
*/
// LPSTR lpstr = new char[1024];
LPWSTR lp = new unsigned short[1024];
// for( int i=0; i<3;i++)
// lp[i] = (str[i*2]+256)*256 + str[i*2+1]+256;
// nLeft = strTemp.GetAt(0) + 256;
// nRight = strTemp.GetAt(1) + 256;
// nCode = ( nLeft<<8 ) + nRight;
memset( lp, 0, 1024 );
// WideCharToMultiByte( CP_UTF8, 0, lp, 3, lpstr, 1024, NULL, NULL );
MultiByteToWideChar( CP_UTF8, 0, str, 100, lp, 1024);
strGB.Empty();
for( int i= 0; i <100; i++ )
{
char c1,c2;
c1 = lp[i]/256-256;
c2 = lp[i]%256-256;
//strGB.Format("%c%c",c1,c2);
strGB += c1 + c2;
}
(發表於2005-2-23 14:01:00) sandy6173:呵呵,我忘記給出UTF-8 轉 GB2312的代碼,對不起啊,代碼如下:
(發表於2005-3-3 9:52:00) sandy6173:char * CXmlProcess::TranslateUTF_8ToGB(char *xmlStream, int len) //len 是xmlStream的長度
{
char * newCharBuffer = new char[len];
int index =0;
int nCBIndex = 0;
while(index < len)
{
if(xmlStream[index] > 0) // 如果是GB2312的字符
{
newCharBuffer[nCBIndex] = xmlStream[index]; //直接復制
index += 1; //源字符串偏移量1
nCBIndex += 1; //目標字符串偏移量1
}
else //如果是UTF-8的字符
{
WCHAR * Wtemp = this->UTF_8ToUnicode(xmlStream + index); //先把UTF-8轉成Unicode
char * Ctemp = this->UnicodeToGB2312(*Wtemp);//再把Unicode 轉成 GB2312
newCharBuffer[nCBIndex] = * Ctemp; // 復制
newCharBuffer[nCBIndex + 1] = *(Ctemp + 1);
index += 3; //源字符串偏移量3
nCBIndex += 2; //目標字符串偏移量2 因為一個中文UTF-8占3個字節,GB2312占兩個字節
}
}
newCharBuffer[nCBIndex] = 0; //結束符
return newCharBuffer;
}
(發表於2005-3-3 9:53:00) Mycro:對於編碼函數,效率要求非常高;
//將2進制數轉換成16進制
static char* BinToHex(char pc);
可以這樣寫:
int CChineseCodeLib::BinToInt(char *pBin)
{
int m_Dec = 0;
int m_Len = strlen(pBin);
for(int i =0 ;i < m_Len ;i ++)
{
m_Dec = (m_Dec << 1) + (pBin[i] - '0');
}
return m_Dec;
}
(發表於2005-4-9 1:19:00) Mycro://看了1個多小時,原來是這個意思
WCHAR* CXmlProcess::UTF_8ToUnicode(char *pText)
{
char char_one;
char char_two;
char char_three;
char uchar[2];
WCHAR *unicode;
char_one = pText[0];
char_two = pText[1]);
char_three = pText[2];
uchar[1] = ((char_one & 0x0F) << 4) + ((char_two >> 2) & 0x0F);
uchar[0] = (char_two & 0x03) << 6 + (char_three & 0x3F);
unicode = (WCHAR *)uchar;
return unicode;
}
(發表於2005-4-9 3:28:00) Mycro://對不起,少加了個擴號
WCHAR* CXmlProcess::UTF_8ToUnicode(char *pText)
{
char uchar[2];
WCHAR *unicode;
char_one = pText[0];
char_two = pText[1]);
char_three = pText[2];
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
unicode = (WCHAR *)uchar;
return unicode;
}
(發表於2005-4-9 3:40:00) zztop5384:int WideCharToMultiByte(
UINT CodePage, // code page
DWORD dwFlags, // performance and mapping flags
LPCWSTR lpWideCharStr, // wide-character string
int cchWideChar, // number of chars in string
LPSTR lpMultiByteStr, // buffer for new string
int cbMultiByte, // size of buffer
LPCSTR lpDefaultChar, // default for unmappable chars
LPBOOL lpUsedDefaultChar // set when default char used
); //將寬字符轉換成多個窄字符
這些只是函數原型,並沒有具體實現
(發表於2005-4-18 10:27:00) zztop5384:幫忙弄以下
(發表於2005-4-18 10:35:00) 鬼龍之舞:支持樓主!是因為你我才寫出來的,不管是在體積還是在速度,相信都比樓主的強一點,如果不考慮移植性的話
感謝樓主!!
UTF8toUnicode proc uses esi edi lpszBuf_OUT,lpszUTF8_IN
mov esi,lpszUTF8_IN
mov edi,lpszBuf_OUT
.while TRUE
mov al,[esi]
.if sbyte ptr al <0
mov al,[esi]
and al,00001111b
shl al,4
mov [edi+1],al
mov al,[esi+1]
and al,00111100b
shr al,2
or [edi+1],al
mov al,[esi+1]
and al,11b
shl al,6
mov [edi+0],al
mov al,[esi+2]
and al,00111111b
or [edi+0],al
add edi,2
add esi,3
.elseif al
xor ah,ah
stosw
inc esi
.else
mov WORD ptr [edi],0
.break
.endif
.endw
ret
UTF8toUnicode endp
(發表於2005-8-25 16:11:00) 鬼龍之舞:UnicodetoUTF8 proc uses esi edi lpBuf_OUT,lpszUTF8_IN
mov esi,lpszUTF8_IN
mov edi,lpBuf_OUT
.while TRUE
mov ax,[esi]
.if ax==0
stosw
.break
.elseif ah==0
add esi,2
stosw
.else
mov al,[esi+1]
shr al,4
or al,11100000b
mov [edi+0],al
mov al,[esi+1]
and al,00001111b
shl al,2
or al,10000000b
mov ah,[esi+0]
shr ah,6
or al,ah
mov [edi+1],al
mov al,[esi+0]
and al,00111111b
or al,10000000b
mov [edi+2],al
add edi,3
add esi,2
.endif
.endw
ret
UnicodetoUTF8 endp
(發表於2005-8-25 16:11:00) 鬼龍之舞:暈.格式沒有了
原文請看
http://www.kbadboy.com/viewfull.asp?id=33
(發表於2005-8-25 16:13:00) robin_fox_nan:shines在2005-2-6,提供了一段程序,里面有
buffersize = WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, unicode, wide_size, NULL, 0, NULL, 0);
buffer = new char[buffersize+1];
但是,我在調試的時候發現:buffersize似乎已經預先留了‘\0’的位置,或者是不是我出錯了
比如:“i love you,愛”GB2312是需要14個字節
UTF8是需要15個字節,返回時候就是這些了啊,
我的地址是:robin-fox@sohu.com,
誰能回答以下,感謝!!
(發表於2006-3-19 20:20:00) kudoo:translateCharToUTF_8的編碼不對,
請作者檢查一下,
如: "你是我的好朋友"
轉換成了;"浣犳槸鎴戠殑濂芥i脲弸鍚?"
正確的應是:
"浣犳槸鎴戠殑濂芥湅鍙嬪悧"
對於有的編碼還能對...
交流一下:kudoo.aos@gmail.com
(發表於2006-8-20 19:46:00) 雁過留聲:[ 原創文檔 本文適合中級讀者 已閱讀34485次 ]
搞笑,這種害人害己的文章還有這么多人訪問。
作者光知道 WideCharToMultiByte 可以把 Unicode 轉成 GB2312 就不知道也可以把 Unicode 轉換為 UTF-8 嗎?
其實這是一個很簡單的程序,都被作者搞復雜了。
要實現 GB2312 (其實是GBK)轉換為 UTF-8 其實很簡單,先用 MultiByteToWideChar 把 GB2312 轉換為 Unicode,再用 WideCharToMultiByte 把 Unicode 轉換為 UTF-8 就可以了。
UTF-8 轉換為 GB2312 是個相反的過程,先用 MultiByteToWideChar 把 UTF-8 轉換為 Unicode,再用 WideCharToMultiByte 把 Unicode 轉換為 GB2312 就可以了。
(發表於2007-1-11 9:11:00) xmwen:/*
字符串編碼轉換 GBK to UTF8 (ansi版)
xmwen@126.com
*/
char *gbk2utf8(const char *strGBK){
int len;
wchar_t *strUnicode;
char *strUTF8;
if (!strGBK){return NULL;}
len = MultiByteToWideChar(CP_GBK, 0,strGBK, -1, NULL,0);
if (len <1){return NULL;}
strUnicode = (wchar_t *) malloc(sizeof(wchar_t) * len);
if (!strUnicode){return NULL;}
len = MultiByteToWideChar(CP_GBK, 0, strGBK, -1, strUnicode, len);
if (len<1){free(strUnicode);return NULL;}
len = WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, NULL, 0, NULL, NULL);
if (len<1){free(strUnicode);return NULL;}
strUTF8 = (char *) malloc(sizeof(char) * len);
if (!strUTF8){free(strUnicode);return NULL;}
len = WideCharToMultiByte (CP_UTF8, 0, strUnicode, -1, strUTF8, len, NULL,NULL);
free(strUnicode);
if (len<1){free(strUTF8);return NULL;}
return strUTF8;
}
(發表於2009-11-3 19:38:00)