常见的中文内码一般有GB2312(简体中文),GBK和台湾那边用的BIG5(繁体中文),有时候看一些台湾编程论坛里的资料,都是乱码,如果在IE中浏览,则要求安装繁体字库的支持。网上也有很多中文内码的转换工具,什么专家,大师,巨匠之类所有光辉灿烂的名字都被使用了,但是在自己的程序中集成这些功能岂不是更好。以前曾广泛流传过使用码表来转换中文内码的Code,但毕竟不完美,而且还要携带或内置一个巨大的表,浪费资源。Windows中提供了MultiByteToWideChar和WideCharToMultiByte两兄弟函数,足可以搞定这些功能了。
以下四个函数分别实现:
大五码转GBK码/GBK转大五码
GB2312码转GBK码/GBK码转GB2312码
于是有人要问了,为什么没有GB2312转BIG5和BIG5转GB2312呢,我们有GBK,可以做一下中转啊。可以将GB2312转成GBK,再将GBK转成BIG5,反之亦然。如果你嫌麻烦,可以自己写一个GB2BIG5/BIG52GB。
//---------------------------------------------------------------------------
// 大五码转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);
}
注意,请不要使用String类的c_str()作为上述几个函数的传入参数。
MSDN范例:
Looking Up a User's Full Name
Computers running Windows Server 2003 family, Windows XP, Windows 2000, and Windows NT can be organized into a domain, which is a collection of computers on a Windows Server 2003 family, Windows 2000 Server, or Windows NT Server network. The domain administrator maintains centralized user and group account information.
To find the full name of a user, given the user name and domain name:
- Convert the user name and domain name to Unicode, if they are not already Unicode strings.
- Look up the computer name of the domain controller (DC) by calling NetGetDCName.
- Look up the user name on the DC computer by calling NetUserGetInfo.
- Convert the full user name to ANSI, unless the program is expecting to work with Unicode strings.
The following sample code is a function (GetFullName) that takes a user name and a domain name in the first two arguments and returns the user's full name in the third argument.
#include <windows.h> #include <lm.h> #include <stdio.h> BOOL GetFullName( char *UserName, char *Domain, char *dest ) { WCHAR wszUserName[UNLEN+1]; // Unicode user name WCHAR wszDomain[256]; LPBYTE ComputerName; struct _SERVER_INFO_100 *si100; // Server structure struct _USER_INFO_2 *ui; // User structure // Convert ANSI user name and domain to Unicode MultiByteToWideChar( CP_ACP, 0, UserName, strlen(UserName)+1, wszUserName, sizeof(wszUserName)/sizeof(wszUserName[0]) ); MultiByteToWideChar( CP_ACP, 0, Domain, strlen(Domain)+1, wszDomain, sizeof(wszDomain)/sizeof(wszDomain[0]) ); // Get the computer name of a DC for the domain. NetGetDCName( NULL, wszDomain, &ComputerName ); // Look up the user on the DC. if( NetUserGetInfo( (LPWSTR) ComputerName, (LPWSTR) &wszUserName, 2, (LPBYTE *) &ui ) ) { printf( "Error getting user information./n" ); return( FALSE ); } // Convert the Unicode full name to ANSI. WideCharToMultiByte( CP_ACP, 0, ui->usri2_full_name, -1, dest, 256, NULL, NULL ); return (TRUE); }
参考:
http://community.youkuaiyun.com/Expert/topic/4310/4310799.xml?temp=.9119684
http://community.youkuaiyun.com/Expert/topic/4370/4370076.xml?temp=.1697657