参考了网上一些方法:所谓的短字符,就是用8bit来表示的字符,典型的应用是ASCII码. 而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE.
常用的代码页有CP_ACP和CP_UTF8两个。
使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。
1. ASCII to Unicode(CP_ACP)
wstring ASCIIToUNICODE(char cArry[]) //传入参数为ANSI串,即用char数组或者string表示的串
{
int nLen = ::MultiByteToWideChar(CP_ACP, 0, cArry, -1, NULL, NULL); //将MultiByteToWideChar()的第四个形参设为-1,即可返回长度
wchar_t *pTemp = new wchar_t[nLen]; //new一个wchar_t空间,保存Unicode串
memset(pTemp, 0, nLen*sizeof(wchar_t));
::MultiByteToWideChar(CP_ACP, 0, cArry, -1, (LPWSTR)pTemp, nLen);
wstring str = pTemp;
if (pTemp)
{
delete [] pTemp;
pTemp = NULL;
}
return str;
}
2. Unicode to ASCII(CP_ACP)
string UNICODEToASCII(wchar_t cArry[]) //传入参数为Unicode串,用“wchar_t cArry[] = {L"这是个测试"};”表示
{
int nLen = ::WideCharToMultiByte(CP_ACP, 0, cArry, -1, NULL, 0, NULL, NULL);
char *pTemp = new char[nLen]; //new 一个char数组,保存ANSI串
memset(pTemp, 0, nLen);
::WideCharToMultiByte(CP_ACP, 0, cArry, -1, pTemp, nLen, NULL, NULL);
string str = pTemp;
if (pTemp)
{
delete [] pTemp;
pTemp = NULL;
}
return str;
}
3. UTF-8 to Unicode(CP_UTF8)
wstring UTF8ToUnicode( const string& str )
{
int unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0 );
wchar_t *pUnicode = new wchar_t[unicodeLen];
memset(pUnicode, 0, unicodeLen*sizeof(wchar_t));
::MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen );
wstring rt = pUnicode;
if(pUnicode )
{
delete [] pUnicode ;
pUnicode = NULL;
}
return rt;
}
4. Unicode to UTF-8(CP_UTF8)
string UnicodeToUTF8( const wstring& str )
{
// wide char to multi char
int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL );
char *pElementText= new char[iTextLen];
memset(pElementText, 0, iTextLen);
::WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL );
string strText;
strText = pElementText;
if(pElementText)
{
delete [] pElementText;
pElementText = NULL;
}
return strText;
}