宽窄字符之间的转换——字符串处理(三)
2010-01-28 15:04
411 查看
前文已经有了简单的描述,这里把其中常用的详细介绍一下。
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
}
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)
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)
相关文章推荐
- C++中数字与字符串之间的转换(包括C++11新标准和宽窄字符转换)
- C++中数字与字符串之间的转换(包括C++11新标准和宽窄字符转换)
- iOS 一个在UDP/TCP通信时,对16进制、10进制、NSData等数据之间的转换发送等处理的工具类库,一行代码将16进制字符串轻松转换成UDP需要的指令数据
- *Delphi的字符串、PChar和字符数组之间的转换
- java中的字符,字符串,数字之间的转换
- 【java基础】在Java中实现基本数据类型与字符、字符串之间的转换
- 字符串字符显示与16进制显示之间的相互转换
- js字符串与字符数组之间的转换
- guava处理字符串与List之间,字符串与map之间的转换
- 将char字符转换成int整型,字符串与整型之间的相互转换
- 宽字符字符串和多字节字符串之间转换
- 串的处理 在实际的开发工作中,对字符串的处理是最常见的编程任务。本题目即是要求程序对用户输入的串进行处理。具体规则如下: 1. 把每个单词的首字母变为大写。 2. 把数字与字母之间用下划线字符(_)分
- 字符串和字符数组之间的转换
- 字符串之间相互转换 字符与数值之间的转换
- javaEE如何处理字符串在java-script-html环境之间的转换使用
- 字符、字符串、数组之间的转换
- VC中一个关于宏的使用问题,字符串之间转换,宽字符与普通字符
- 宽窄字符之间的转换
- 字节数组 整型 字符串 十六进制 字符 之间的转换
- 字符串与字符数组之间的转换(JAVA程序设计基础篇9.2.9)