UTF-8与GB2312之间的互换,UTF-8与GB2312之间的互换
【 tulaoshi.com - C语言心得技巧 】
int WideCharToMultiByte(UINT CodePage, // code pageDWORD dwFlags, // performance and mapping flagsLPCWSTR lpWideCharStr, // wide-character stringint cchWideChar, // number of chars in stringLPSTR lpMultiByteStr, // buffer for new stringint cbMultiByte, // size of bufferLPCSTR lpDefaultChar, // default for unmappable charsLPBOOL lpUsedDefaultChar // set when default char used); //将宽字符转换成多个窄字符int MultiByteToWideChar(UINT CodePage, // code pageDWORD dwFlags, // character-type optionsLPCSTR lpMultiByteStr, // string to mapint cbMultiByte, // number of bytes in stringLPWSTR lpWideCharStr, // wide-character bufferint 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_thr
来源:http://www.tulaoshi.com/n/20160129/1485357.html
看过《UTF-8与GB2312之间的互换》的人还看了以下文章 更多>>