![]() |
新闻 | 体育 | 财经 | 娱乐 | 商业 | 科技 | 汽车 | 数码 | 女人 | 旅游 | 教育 | 文化 | 广州 | 军事 | 部落 评论 | NBA | 明星 | 证券 | 基金 | 探索 | 房产 | 手机 | 两性 | 健康 | 培训 | 电影 | 游戏 | 拍卖 | 论坛 |
| 科技首页 | 重磅报道 | 专题 | IT茶馆 | 技术趋势 | 科学 | 学院 | 游戏 | 病毒 | 下载 | 论坛 | ![]() |
常见的中文内码一般有GB2312(简体中文),GBK和台湾那边用的BIG5(繁体中文),有时候看一些台湾编程论坛里的资料,都是乱码,如果在IE中浏览,则要求安装繁体字库的支持。网上也有很多中文内码的转换工具,什么专家,大师,巨匠之类所有光辉灿烂的名字都被使用了,但是在自己的程序中集成这些功能岂不是更好。以前曾广泛流传过使用码表来转换中文内码的Code,但毕竟不完美,而且还要携带或内置一个巨大的表,浪费资源。Windows中提供了MultiByteToWideChar和WideCharToMultiByte两兄弟函数,足可以搞定这些功能了。
//---------------------------------------------------------------------------
// 大五码转GBK码:
// い地チ㎝瓣 --> 中華人民共和國
void __fastcall 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转大五码
// 中華人民共和國 --> い地チ㎝瓣
void __fastcall 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 __fastcall 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 __fastcall 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;
}
}
//---------------------------------------------------------------------------
// 测试代码
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char szBuf[255];
// 从GB2312转到GBK
strcpy(szBuf, Edit1->Text.c_str());
GB2GBK(szBuf);
Edit2->Text = String(szBuf);
// 从GB2312转到BIG5,通过GBK中转
strcpy(szBuf, Edit1->Text.c_str());
GB2GBK(szBuf);
GBK2BIG5(szBuf);
Edit3->Text = String(szBuf);
}
[编辑:gigi_miao] [返回首页]
推荐文章: