1、chs2cht.h
#pragma once char* UnicodeToBIG5(const wchar_t* szUnicodeString); char* UnicodeToGB2312(const wchar_t* szUnicodeString); wchar_t* GB2312ToUnicode(const char* szGBString); char* GB2312ToBIG5(const char* szGBString);
2、chs2cht.cpp
#include "chs2cht.h" #include <atlstr.h> //GB2312 转 Unicode: wchar_t* GB2312ToUnicode(const char* szGBString) { UINT nCodePage = 936; //GB2312 int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0); wchar_t* pBuffer = new wchar_t[nLength+1]; MultiByteToWideChar(nCodePage,0,szGBString,-1,pBuffer,nLength); pBuffer[nLength]=0; return pBuffer; } //BIG5 转 Unicode: wchar_t* BIG5ToUnicode(const char* szBIG5String) { UINT nCodePage = 950; //BIG5 int nLength=MultiByteToWideChar(nCodePage,0,szBIG5String,-1,NULL,0); wchar_t* pBuffer = new wchar_t[nLength+1]; MultiByteToWideChar(nCodePage,0,szBIG5String,-1,pBuffer,nLength); pBuffer[nLength]=0; return pBuffer; } //Unicode 转 GB2312: char* UnicodeToGB2312(const wchar_t* szUnicodeString) { UINT nCodePage = 936; //GB2312 int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL); char* pBuffer=new char[nLength+1]; WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL); pBuffer[nLength]=0; return pBuffer; } //Unicode 转 BIG5: char* UnicodeToBIG5(const wchar_t* szUnicodeString) { UINT nCodePage = 950; //BIG5 int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL); char* pBuffer=new char[nLength+1]; WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL); pBuffer[nLength]=0; return pBuffer; } //繁体中文BIG5 转 简体中文GB2312 char* BIG5ToGB2312(const char* szBIG5String) { LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC); wchar_t* szUnicodeBuff = BIG5ToUnicode(szBIG5String); char* szGB2312Buff = UnicodeToGB2312(szUnicodeBuff); int nLength = LCMapStringA(lcid,LCMAP_SIMPLIFIED_CHINESE, (LPCSTR)szGB2312Buff,-1,NULL,0); char* pBuffer = new char[nLength + 1]; LCMapStringA(0x0804,LCMAP_SIMPLIFIED_CHINESE,(LPCSTR)szGB2312Buff,-1,pBuffer,nLength); pBuffer[nLength] = 0; delete[] szUnicodeBuff; delete[] szGB2312Buff; return pBuffer; } //简体中文GB2312 转 繁体中文BIG5 char* GB2312ToBIG5(const char* szGBString) { LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC); int nLength = LCMapStringA(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,NULL,0); char* pBuffer=new char[nLength+1]; LCMapStringA(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,pBuffer,nLength); pBuffer[nLength]=0; wchar_t* pUnicodeBuff = GB2312ToUnicode(pBuffer); char* pBIG5Buff = UnicodeToBIG5(pUnicodeBuff); delete[] pBuffer; delete[] pUnicodeBuff; return pBIG5Buff; }
3、test.cpp
#include "chs2cht.h" #include <iostream> #include <locale.h> #include <atlstr.h> using namespace std; int main(int argc, char** argv) { //“区域设置”为简体中文 locale loc( "chs" ); char str[100]; cin>>str; char * rlt=GB2312ToBIG5(str); CString cStr1; cStr1.Format( TEXT("%s"),rlt); //“区域设置”为繁体中文 setlocale(LC_ALL, ".950"); cout<<rlt<<endl; return 0; }