// Chinesecode. h: interface for the cchinesecode class.
//
//////////////////////////////////////// //////////////////////////////
# If! Defined (afx_chinesecode_h1_e9206178_ca96_4b8a_ab2a_ad94c9cd12b11_encoded _)
# Define afx_chinesecode_h1_e9206178_ca96_4b8a_ab2a_ad94c9cd12b11_encoded _
# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
# Include <string>
Using namespace STD;
Class chinesecode
{
Public:
Static void utf_8tounicode (wchar_t * pout, char * ptext); // converts the UTF-8 to Unicode
Static void unicodetoutf_8 (char * pout, wchar_t * ptext); // Unicode to UTF-8
Static void unicodetogb2312 (char * pout, wchar_t udata); // converts Unicode to gb2312
Static void gb2312tounicode (wchar_t * pout, char * gbbuffer); // convert gb2312 to Unicode
Static void gb2312toutf_8 (string & pout, char * ptext, int Plen); // convert gb2312 to UTF-8
Static void utf_8togb2312 (string & pout, char * ptext, int Plen); // convert the UTF-8 to gb2312
};
# Endif //! Defined (afx_chinesecode_h1_e9206178_ca96_4b8a_ab2a_ad94c9cd12b11_encoded _)
// Chinesecode. cpp: Implementation of the chinesecode class.
//
//////////////////////////////////////// //////////////////////////////
# Include "stdafx. H"
# Include "chinesecode. H"
//////////////////////////////////////// //////////////////////////////
// Construction/destruction
//////////////////////////////////////// //////////////////////////////
Void chinesecode: utf_8tounicode (wchar_t * pout, char * ptext)
{
Char * uchar = (char *) pout;
Uchar [1] = (ptext [0] & 0x0f) <4) + (ptext [1]> 2) & 0x0f );
Uchar [0] = (ptext [1] & 0x03) <6) + (ptext [2] & 0x3f );
Return;
}
Void chinesecode: unicodetoutf_8 (char * pout, wchar_t * ptext)
{
// Pay attention to the order of wchar high and low characters. The lower byte is in the front and the higher byte is in the back
Char * pchar = (char *) ptext;
Pout [0] = (0xe0 | (pchar [1] & 0xf0)> 4 ));
Pout [1] = (0x80 | (pchar [1] & 0x0f) <2) + (pchar [0] & 0xc0)> 6 );
Pout [2] = (0x80 | (pchar [0] & 0x3f ));
Return;
}
Void chinesecode: unicodetogb2312 (char * pout, wchar_t udata)
{
Widechartomultibyte (cp_acp, null, & udata, 1, pout, sizeof (wchar_t), null, null );
Return;
}
Void chinesecode: gb2312tounicode (wchar_t * pout, char * gbbuffer)
{
: Multibytetowidechar (cp_acp, mb_precomposed, gbbuffer, 2, pout, 1 );
Return;
}
Void chinesecode: gb2312toutf_8 (string & pout, char * ptext, int Plen)
{
Char Buf [4];
Int nlength = Plen * 3;
Char * rst = new char [nlength];
Memset (BUF, 0, 4 );
Memset (RST, 0, nlength );
Int I = 0;
Int J = 0;
While (I <Plen)
{
// Directly copy data in English
If (* (ptext + I)> = 0)
{
RST [J ++] = ptext [I ++];
}
Else
{
Wchar_t pbuffer;
Gb2312tounicode (& pbuffer, ptext + I );
Unicodetoutf_8 (BUF, & pbuffer );
Unsigned short int TMP = 0;
TMP = rst [J] = Buf [0];
TMP = rst [J + 1] = Buf [1];
TMP = rst [J + 2] = Buf [2];
J + = 3;
I + = 2;
}
}
RST [J] = '\ 0 ';
// Return results
Pout = RST;
Delete [] RST;
Return;
}
Void chinesecode: utf_8togb2312 (string & pout, char * ptext, int Plen)
{
Char * newbuf = new char [Plen];
Char ctemp [4];
Memset (ctemp, 0, 4 );
Int I = 0;
Int J = 0;
While (I <Plen)
{
If (ptext [I]> 0)
{
Newbuf [J ++] = ptext [I ++];
}
Else
{
Wchar wtemp;
Utf_8tounicode (& wtemp, ptext + I );
Unicodetogb2312 (ctemp, wtemp );
Newbuf [J] = ctemp [0];
Newbuf [J + 1] = ctemp [1];
I + = 3;
J + = 2;
}
}
Newbuf [J] = '\ 0 ';
Pout = newbuf;
Delete [] newbuf;
Return;
}