#include <Windows.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// Big5 => GBK:
// い地チ㎝瓣 --> 中華人民共和國
void BIG52GBK(char *szBuf)
{
if(!strcmp(szBuf, ""))
return;
int nStrLen = strlen(szBuf);
wchar_t *pws = new wchar_t[nStrLen + 1];
__try
{
int nReturn = MultiByteToWideChar(950, 0, szBuf, nStrLen, pws, nStrLen + 1);
BOOL bValue = false;
nReturn = WideCharToMultiByte(936, 0, pws, nReturn, szBuf, nStrLen + 1, "?", &bValue);
szBuf[nReturn] = 0;
}
__finally
{
delete[] pws;
}
}
//---------------------------------------------------------------------------
// GBK => Big5
// 中華人民共和國 --> い地チ㎝瓣
void GBK2BIG5(char *szBuf)
{
if(!strcmp(szBuf, ""))
return ;
int nStrLen = strlen(szBuf);
wchar_t *pws = new wchar_t[nStrLen + 1];
__try
{
MultiByteToWideChar(936, 0, szBuf, nStrLen, pws, nStrLen + 1);
BOOL bValue = false;
WideCharToMultiByte(950, 0, pws, nStrLen, szBuf, nStrLen + 1, "?", &bValue);
szBuf[nStrLen] = 0;
}
__finally
{
delete[] pws;
}
}
//----------------------------------------------------------------------------
// GB2312 => GBK
// 中华人民共和国 --> 中華人民共和國
void GB2GBK(char *szBuf)
{
if(!strcmp(szBuf, ""))
return;
int nStrLen = strlen(szBuf);
WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
int nReturn = LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nStrLen, NULL, 0);
if(!nReturn)
return;
char *pcBuf = new char[nReturn + 1];
__try
{
wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nReturn, pcBuf, nReturn + 1);
strncpy(szBuf, pcBuf, nReturn);
}
__finally
{
delete[] pcBuf;
}
}
//---------------------------------------------------------------------------
// GBK =〉GB2312
// 中華人民共和國 --> 中华人民共和国
void GBK2GB(char *szBuf)
{
if(!strcmp(szBuf, ""))
return;
int nStrLen = strlen(szBuf);
WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
int nReturn = LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nStrLen, NULL, 0);
if(!nReturn)
return;
char *pcBuf = new char[nReturn + 1];
__try
{
wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nReturn, pcBuf, nReturn + 1);
strncpy(szBuf, pcBuf, nReturn);
}
__finally
{
delete [] pcBuf;
}
}
int main()
{
// WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL), SORT_CHINESE_PRC);
//
// SetLocaleInfoA(wLCID, LOCALE_USE_CP_ACP, "");
char sourceEncode[255] = "宋体";
char szBuf[1024];
// // 从 GB2312 转到 GBK
// strcpy(szBuf, sourceEncode);
// GB2GBK(szBuf);
// printf(szBuf);
// printf("\n");
// // 从GB2312 转到 BIG5,通过 GBK 中转
//strcpy(szBuf, sourceEncode);
//GB2GBK(szBuf);
//printf(szBuf);
//printf("\n");
//GBK2BIG5(szBuf);
//strcpy(szBuf, sourceEncode);
//BIG52GBK(szBuf);
//printf(szBuf);
//printf("\n");
HANDLE hFile = CreateFile("F:\\Games\\DJXOnline\\bin\\复件 Main.lang", GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
assert(hFile != INVALID_HANDLE_VALUE);
DWORD dwSize = GetFileSize(hFile, NULL);
assert(dwSize);
LPSTR lpStr = new char[dwSize+10];
assert(lpStr);
LPSTR lpBuff = new char[dwSize * 5];
ZeroMemory(lpStr, dwSize+10);
ZeroMemory(lpBuff, dwSize * 5);
DWORD nreadsize;
ReadFile(hFile, lpStr, dwSize, &nreadsize, NULL);
strcpy(lpBuff, lpStr);
BIG52GBK(lpBuff);
printf(lpBuff);
printf("\n");
delete [] lpBuff;
delete [] lpStr;
}