您的位置:首页 > Web前端

宽窄字符之间的转换——字符串处理(三)

2010-01-28 15:04 411 查看
前文已经有了简单的描述,这里把其中常用的详细介绍一下。

1. Window字符串LPSTR <—> LPWSTR

char * <> wchar_t *
char * <> unsigned short *
IN32 API里没有符合这种要求的函数,但可以自己进行封装。主要是对API函数MultiByteToWideChar和WideCharToMultiByte的封装。
 
 
LPCSTR—>LPWSTR
 
 
//-------------------------------------------------------------------------------------
//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;
}
 
 
LPCWSTR —>LPSTR
 
//-------------------------------------------------------------------------------------
//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]));
 
这两个函数的缺点在于无法动态分配内存,在转换很长的字符串时可能会浪费较多内存空间;优点是在不考虑浪费空间的情况下转换较短字符串非常方便。
 
为了解决不能动态分配内存的问题,重新定义如下:
 
LPWSTR MByteToWChar(LPCSTR lpcszStr)
{
    // Get the required size of the buffer that receives the Unicode
    // string.
       if(lpcszStr == NULL)
              return NULL;
 
    DWORD dwMinSize;
    dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
      
       LPWSTR lpwszStr = new wchar_t[dwMinSize];/
      
       if (!lpwszStr)
       {
              delete [] lpwszStr;
              return NULL;
       }
   
    // Convert headers from ASCII to Unicode.
    MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize); 
    return lpwszStr;
}
 
LPSTR WCharToMByte(LPCWSTR lpcwszStr)
{
       if (lpcwszStr == NULL)
              return NULL;
 
       DWORD dwMinSize;
       dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
      
       LPSTR lpszStr = new char[dwMinSize];
      
       if(!lpszStr)
       {
              delete [] lpszStr;
              return NULL;
       }
       WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwMinSize,NULL,FALSE);
       return lpszStr;
}
应用也比较简单:
char sText[20] = {"多字节字符串!OK!"};
wchar_t * pwchar = MByteToWChar(sText);
char * pchar = WCharToMByte(pwchar);
delete pwchar;
delete pchar;
 
因为是在函数内部动态分配内存,因此在外部必须delete掉,否者会产生内存泄露
 
为了方便使用可以利用namespace来实现
namespace _strconv_util
{
       // definitions of MByteToWChar and WCharToMByte
}

2. char* <—> BSTR

 
Char * > BSTR
namespace _com_util {
       // Convert char * to BSTR
       //
       BSTR __stdcall ConvertStringToBSTR(const char* pSrc) throw(_com_error);
 
       // Convert BSTR to char *
       //
       char* __stdcall ConvertBSTRToString(BSTR pSrc) throw(_com_error);
}
 
 
例如
_com_util::ConvertStringToBSTR(strKeyName.c_str())
 
 
这个也可用
const char* cstrAttributeValue
BSTR bstr = _bstr_t(cstrAttributeValue)
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐