wchar char 互转

来源:互联网 发布:centos 6.8下载地址 编辑:程序博客网 时间:2024/05/21 12:45
//功能:将char字符串转为wchar 字符串,
//可以两次调用,第一次 lpwszStr = NULL or dwCount = 0;获得所需长度在dwCount中。
//返回值:0成功 1缓冲区不够重分配,-1失败
DWORD MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD &dwCount)
{
DWORD dwMinSize;
DWORD dwRet = 0;


do 
{
if (NULL == lpcszStr)
{
dwCount = 0;
dwRet =  -1;
break;
}
//得到所需长度
dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
if(dwCount < dwMinSize || NULL == lpwszStr)
{
dwCount = dwMinSize;
dwRet = 1;
break;
}


// 转换
dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize); 
if (0 == dwMinSize)
{
dwRet = -1;
break;
}


} while (FALSE);


return dwRet;
}


//功能:将wchar字符串转为wchar char字符串,
//      可以两次调用,第一次 lpszStr = NULL or dwCount = 0;获得所需长度在dwCount中。
//返回值:0成功 1缓冲区不够重分配,-1失败
DWORD WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD &dwCount)
{
DWORD dwMinSize;
DWORD dwRet = 0;


do 
{
if (NULL == lpcwszStr)
{
dwCount = 0;
dwRet =  -1;
break;
}


dwMinSize = WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, NULL, 0, NULL, FALSE);
if(dwCount < dwMinSize || NULL == lpszStr)
{
dwCount = dwMinSize;
dwRet =  1;
break;
}


dwMinSize = WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, lpszStr, dwCount, NULL, FALSE);
if (0 == dwMinSize)
{
dwRet = -1;
break;
}


} while (FALSE);



return dwRet;
}




如果之前我们已经分配好空间,并且由于字符串较短,可以不理会浪费的空间,仅仅只是想简单地将短字符和宽字符相互转换,那有没有什么简便的方法呢?
WIN32 API里没有符合这种要求的函数,但我们可以自己进行封装:








    
//-------------------------------------------------------------------------------------
//Description:
// This function maps a character string to a wide-character (Unicode) string
//
//Parameters:
// lpcszStr: [in] Pointer to the character string to be converted 
// lpwszStr: [out] Pointer to a buffer that receives the translated string. 
// dwSize: [in] Size of the buffer
//
//Return Values:
// TRUE: Succeed
// FALSE: Failed
// 
//Example:
// MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
//---------------------------------------------------------------------------------------
BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
{
    // Get the required size of the buffer that receives the Unicode 
    // string. 
    DWORD dwMinSize;
    dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);


    if(dwSize < dwMinSize)
    {
     return FALSE;
    }


   
    // Convert headers from ASCII to Unicode.
    MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize); 
    return TRUE;
}


//-------------------------------------------------------------------------------------
//Description:
// This function maps a wide-character string to a new character string
//
//Parameters:
// lpcwszStr: [in] Pointer to the character string to be converted 
// lpszStr: [out] Pointer to a buffer that receives the translated string. 
// dwSize: [in] Size of the buffer
//
//Return Values:
// TRUE: Succeed
// FALSE: Failed
// 
//Example:
// MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
//---------------------------------------------------------------------------------------
BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
{
   DWORD dwMinSize;
   dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
   if(dwSize < dwMinSize)
   {
    return FALSE;
   }
   WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
   return TRUE;
}




使用方法也很简单,示例如下:
wchar_t wText[10] = {L"函数示例"};
char sText[20]= {0};
WCharToMByte(wText,sText,sizeof(sText)/sizeof(sText[0]));
MByteToWChar(sText,wText,sizeof(wText)/sizeof(wText[0]));


这两个函数的缺点在于无法动态分配内存,在转换很长的字符串时可能会浪费较多内存空间;优点是,在不考虑浪费空间的情况下转换较短字符串非常方便.



wchar 中文字串显示语言设置



 LCID lcid = GetThreadLocale(); 
 LCID tID  = lcid; 
 
 lcid &= 0xFF; 
 switch(lcid) 
 { 
 default: 
 case LANG_ENGLISH: 
 _tsetlocale(LC_CTYPE, _T("english"));   
 break; 
 case LANG_CHINESE: 
 { 
 tID = tID >> 10; 
 switch(tID) 
 { 
 case SUBLANG_CHINESE_TRADITIONAL: 
 _tsetlocale(LC_CTYPE, _T("cht"));   
 break; 
 case SUBLANG_CHINESE_SIMPLIFIED: 
 _tsetlocale(LC_CTYPE, _T("chs"));   
 break; 
 } 
 } 
 }