Believe that there must be a lot of program developers often encounter the problem of character encoding, and this problem is very annoying. Because these are potential mistakes, it is necessary to develop experience in this area to find out these mistakes. Especially in the processing of XML documents, the problem appears more frequent, there is a Java write Server program, with VC write client interaction. The protocols that interact are written in XML. As a result, it is always found that data is not accepted correctly in communication. Wonder! Then use the Crawl Network packet tool to crawl data, then found that the original Java XML on the head is such a <?xml version= "1.0" encoding= "UTF-8", and VC on the default is GB2312. So the first encounter Chinese character data is not correct. To find information on the Internet, this article seems particularly small, for such issues as this, I would like to introduce myself to write a conversion program. Of course, the procedure is simple. If there is the superfluous place, also hope that you master laugh.
If you are unfamiliar with UTF-8, Unicode, GB2312 and so on, please check, I don't waste my breath here. Here are two functions for WINAPI: WideCharToMultiByte, MultiByteToWideChar.
Function Prototypes:
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
);//将多个窄字符转换成宽字符
Some of the functions you need to use:
CString Cxmlprocess::hextobin (CString string)//Converts the 16 number into 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)//Converts the 2 number into 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 character data into a 10-integer
{
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;
}