Chinese internal code conversion simple algorithm

xiaoxiao2021-03-06  195

Chinese internal code conversion class

??? Chinese is divided into Simplified, Traditional, and there is also a unified code. If you want to convert between different codes, you need to have a corresponding form, and the program is very cumbersome, and there is a built-in control form. The author, I found that as long as the MULTIBYTOWIDECHAR and LCMAPSTRING two functions can be simply implemented. In order to make the programmer more convenient, the author wrote a CCHINESECONVERTOR class and packaged all the Chinese internal code conversion function. The reader can join this in the MFC directly, it is very easy to use. Detailed details Please see Chineseconvertor.h and Chineseconvertor.cpp source code.

This source code complies with the GNU unpaid to the reader, no copyright restrictions.

Zhao Xiang

#pragma overce #include "afx.h"

class CChineseConvertor:? public CObject {public:? CChineseConvertor (void) ;? ~ CChineseConvertor (void) ;? CString Big52GBKSimplified (CString szText) ;? CString Big52GBKTraditional (CString szText) ;? CString GBK2Big5 (CString szText) ;? LPTSTR GBKSimplified2GBKTraditional ( Cstring szsimplified ;? lptstr GBKTRADITAL2GBKSIMPLIFIED (CSTRING SZTRADIAL);

CHAR * m_pszunknown;? // Convert to Unicode? LPWSTR TOUNICODE (CSTRING SZSOURCE, INT NENCODING); LPTSTR SZSource, int Nencoding;

#include "stdafx.h" #include "chineseconvertor.h"

CCHINESECONVERTOR :: CCHINESECONVERTOR (VOID) {? M_pszunknown = new char [2] ;? m_pszunknown [0] = '?' ;? m_pszunknown [1] = 0;}

CCHINESECONVERTOR :: ~ cchineseconvertor (void) {? Delete m_pszunknown;}

CSTRING CCHINESECONVERTOR :: BIG52GBKSIMPLIFIED (CSTRING SZTEXT) {? Int NLENGTH;? Wchar_t * pBuffer;? Lpstr preSult; int NRESulTlength

? NLength = MultiByteToWideChar (950,0, szText, szText.GetLength (), NULL, 0) ;? pBuffer = new wchar_t [nLength 1] ;? MultiByteToWideChar (950,0, (LPCTSTR) szText, szText.GetLength () , (Lpwstr) PBUFFER, NLENGTH);? PBUFFER [NLENGTH] = 0;

? NResultLength = WideCharToMultiByte (936,0, pBuffer, nLength, NULL, 0, m_pszUnknown, FALSE) ;? pResult = new char [nResultLength 1] ;? WideCharToMultiByte (936,0, (LPWSTR) pBuffer, nLength, (LPSTR) PRESULT, NRESULTLENGTH, "?", false) ;? presult [NRESULTLENGTH] = 0;? Return GBKTRADITIONAL2GBKSIMPLIFIED (PRESULT);?}

CSTRING CCHINESECONVERTOR :: Big52gbktradories (cstring sztext) {? Int NLENGTH;? Wchar_t * pBuffer;? Lpstr preSult;? Int NRESulTlength

? NLength = MultiByteToWideChar (950,0, szText, szText.GetLength (), NULL, 0) ;? pBuffer = new wchar_t [nLength 1] ;? MultiByteToWideChar (950,0, (LPCTSTR) szText, szText.GetLength () , (Lpwstr) PBUFFER, NLENGTH);? PBUFFER [NLENGTH] = 0;

? NResultLength = WideCharToMultiByte (936,0, pBuffer, nLength, NULL, 0, m_pszUnknown, FALSE) ;? pResult = new char [nResultLength 1] ;? WideCharToMultiByte (936,0, (LPWSTR) pBuffer, nLength, (LPSTR) PRESULT, NRESULTLENGTH, "?", false) ;? presult [NRESULTLENGTH] = 0;

Return PRESULT;

LPTSTR CChineseConvertor :: GBKTraditional2GBKSimplified (CString szTraditional) {LCID dwLocale;?? WORD wLangID;? WLangID = MAKELANGID (LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED) ;? dwLocale = MAKELCID (wLangID, SORT_CHINESE_PRC);

? Int nLength;? Char * pBuffer;? NLength = LCMapString (dwLocale, LCMAP_SIMPLIFIED_CHINESE, (LPCTSTR) szTraditional, szTraditional.GetLength (), NULL, 0) ;? pBuffer = new char [nLength 1] ;? pBuffer [nLength] = 0; lcmapstring (dwlocale, lcmap_simplified_chinese, (lpctstr) sztraditional, sztraditional.getlength (), pbuffer, nlength) ;? Return PBuffer;}

CSTRING CCHINESECONVERTOR :: GBK2BIG5 (CSTRING SZTEXT) {? Lptstr szgbktraditional;? Int NLENGTH;? Wchar_t * pBuffer;? Lpstr presult; int NRESULTLENGTH;

? SzGBKTraditional = GBKSimplified2GBKTraditional (szText) ;? nLength = MultiByteToWideChar (936,0, szGBKTraditional, strlen (szGBKTraditional), NULL, 0) ;? pBuffer = new wchar_t [nLength 1] ;? MultiByteToWideChar (936,0, (LPCTSTR) szGBKTraditional, strlen (szGBKTraditional), (LPWSTR) pBuffer, nLength) ;? pBuffer [nLength] = 0;? nResultLength = WideCharToMultiByte (950,0, pBuffer, nLength, NULL, 0, m_pszUnknown, FALSE) ;? pResult = new char [NResultlength 1];? Widechartomultibyte (950, 0, (lpwstr) PBuffer, NLENGTH, (LPSTR) PRESULT, NRESULTLENGTH, "?", false ;? presult [NRESULTLENGTH] = 0;

Return PRESULT;

// Convert to GBK GBK Simplified Traditional LPTSTR CChineseConvertor :: GBKSimplified2GBKTraditional (CString szSimplified) {LCID dwLocale;?? WORD wLangID;? WLangID = MAKELANGID (LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED) ;? dwLocale = MAKELCID (wLangID, SORT_CHINESE_PRC);

? Int nLength;? Char * pBuffer;? NLength = LCMapString (dwLocale, LCMAP_TRADITIONAL_CHINESE, (LPCTSTR) szSimplified, szSimplified.GetLength (), NULL, 0) ;? pBuffer = new char [nLength 1] ;? pBuffer [nLength] = 0; lcmapstring (dwlocale, lcmap_traditional_chinese, (lpctstr) szSIMPLified, szSIMPLified.getLength (), PBuffer, NLENGTH);? Return PBUFFER;}

// Convert to Unicodelpwstr CchineeSeconvertor :: Tounicode (cstring szsource, int nens; int NLENGTH;? Wchar_t * pBuffer; int nlanguage;

? IF (neencoding == Chinese_simplified ?? nLANGUAGE = 936;? Else ?? IF (neencoding == Chinese_traditional) ??? nLANGUAGE = 950; ?? Else ??? NLANGUAGE = CP_ACP;

? NLength = MultiByteToWideChar (nLanguage, 0, szSource, szSource.GetLength (), NULL, 0) ;? pBuffer = new wchar_t [nLength 1] ;? MultiByteToWideChar (nLanguage, 0, (LPCTSTR) szSource, szSource.GetLength () , (Lpwstr) PBUFFER, NLENGTH);? PBUFFER [NLENGTH] = 0;

? Return PBuffer;}

LPTSTR CChineseConvertor :: ToMultiByte (LPWSTR szSource, int nEncoding) {int nLength;?? Char * pBuffer;? Int nLanguage;? If (nEncoding == CHINESE_SIMPLIFIED) ?? nLanguage = 936;? Else ?? if (nEncoding == CHINESE_TRADITIONAL ) ??? NLANGUAGE = 950; ?? Else ??? NLANGUAGE = CP_ACP;

NLENGTH = Widechartomultibyte (NLANGUAGE, 0, SZSource, WCSLEN (Szsource), NULL, 0, M_PSZUNKNOWN, FALSE

PBUFFER = New char [NLENGTOMULTIBYTE (NLANGUAGE, 0, SZSOUR, WCSLEN (SZSource), PBuffnown, False, PBuffer [NLENGTH] = 0;

Return PBuffer;

}

转载请注明原文地址:https://www.9cbs.com/read-128265.html

New Post(0)