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

前文已经有了简单的描述,这里把其中常用的详细介绍一下。

1. Window字符串LPSTR <> LPWSTR

char * <> wchar_t *

char * <> unsigned short *

IN32 API里没有符合这种要求的函数,但可以自己进行封装。主要是对API函数MultiByteToWideCharWideCharToMultiByte的封装。

 

 

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)

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值