三种常见中文内码的转换方法
Posted 0x16
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三种常见中文内码的转换方法相关的知识,希望对你有一定的参考价值。
我们平时常见的三种中文内码是:GB2312(简体中文)、GBK、BIG5(繁体中文)。网上有很多中文内码的专用转换工具。我们碰到由于内码不一致而导致的乱麻问题,用这些工具可以进行相互转换。但论坛里经常有人问如何在自己的程序中集成这些功能呢?本文将介绍如何利用 Windows 提供的API 函数来实现。转换涉及到的 API 函数主要有两个:MultiByteToWideChar 和 WideCharToMultiByte。有关这两个函数的详细文档请参考 MSDN,本文不再赘述。
本文将介绍四个转换函数分别实现如下的转换:
* Big5 => GBK
* GBK => Big5
* GB2312 => GBK
* GBK => GB2312
有关 GB2312 =〉BIG5 的转换以及 BIG5 =〉GB2312 的转换可以通过 GBK 间接实现。先将 GB2312 转成 GBK,再将 GBK 转成 BIG5,反之亦然。当然也可以自己实现它们之间的直接转换。
// 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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
}
{
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;
}
}
// 调用示例
......
char sourceEncode[ 255 ];
char szBuf[ 1024 ];
// 从 GB2312 转到 GBK
strcpy(szBuf, sourceEncode);
GB2GBK(szBuf);
// 从GB2312 转到 BIG5,通过 GBK 中转
strcpy(szBuf, sourceEncode);
GB2GBK(szBuf);
GBK2BIG5(szBuf);
......
}
char sourceEncode[ 255 ];
char szBuf[ 1024 ];
// 从 GB2312 转到 GBK
strcpy(szBuf, sourceEncode);
GB2GBK(szBuf);
// 从GB2312 转到 BIG5,通过 GBK 中转
strcpy(szBuf, sourceEncode);
GB2GBK(szBuf);
GBK2BIG5(szBuf);
......
}
以上是关于三种常见中文内码的转换方法的主要内容,如果未能解决你的问题,请参考以下文章