比较有用的字符串编码转换类

#include <iostream>
using namespace std;
#include <comdef.h>

string wstring2string(wstring wstr)
{
    
    
	string result;
	int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
	if( len <= 0 )return result;
	char* buffer = new char[len + 1];
	if(buffer == NULL )return result;
	WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
	buffer[len] = '\0';
	result.append(buffer);
	delete[] buffer;
	return result;
}
wstring string2wstring(string str)
{
    
    
	wstring result;
	int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
	if( len < 0 )return result;
	wchar_t* buffer = new wchar_t[len + 1];
	if( buffer == NULL )return result;
	MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
	buffer[len] = '\0';
	result.append(buffer);
	delete[] buffer;
	return result;
}

StringUtil.h

typedef wchar_t *BSTR;

namespace StringUtils
{
    
    
	// Summary: 
	//	 unicode 转 ansi
	// Parameters:
	//	 pUnicode	- [IN] 要转换的unicode字符串
	//	 pAnsiBuffer	- [OUT] 转换后的字符串保存位置
	//	 nBufferSize	- [IN] pAnsiBuffer的长度(char或wchar_t个数)
	// Returns:
	//	 转换生成的字符串长(含尾部0)。如果nBufferSize为0且pAnsiBuffer为NULL,可以得到转换所需的字符串长(也包含尾部0)
	// See also:
	//	 Ansi2Unicode, Unicode2UTF8, UTF82Unicode
	int Unicode2Ansi(const wchar_t* pUnicode, char* pAnsiBuffer, int nBufferSize);

	// Summary: 
	//	 ansi 转 unicode
	// Parameters:
	//	 pAnsi		- [IN] 要转换的ansi字符串
	//	 pUnicodeBuffer	- [OUT] 转换后的字符串保存位置
	//	 nBufferSize	- [IN] pUnicodeBuffer的长度(wchar_t个数)
	// Returns:
	//	 转换生成的字符串长(含尾部0)。如果nBufferSize为0且pUnicodeBuffer为NULL,可以得到转换所需的字符串长(也包含尾部0)
	// See also:
	//	 Unicode2Ansi, Unicode2UTF8, UTF82Unicode
	int Ansi2Unicode(const char* pAnsi, wchar_t* pUnicodeBuffer, int nBufferSize);

	// Summary: 
	//	 unicode 转 utf8
	// Parameters:
	//	 pUnicode	- [IN] 要转换的unicode字符串
	//	 pUTF8		- [IN] 要转换的utf8字符串
	//	 pUTF8Buffer	- [OUT] 转换后的字符串保存位置
	//	 nBufferSize	- [IN] pUTF8Buffer的长度(char个数)
	// Returns:
	//	 转换生成的字符串长(含尾部0)。如果nBufferSize为0且pUTF8Buffer为NULL,可以得到转换所需的字符串长(也包含尾部0)
	// See also:
	//	 Ansi2Unicode, Unicode2Ansi, UTF82Unicode
	int Unicode2UTF8(const wchar_t* pUnicode, char* pUTF8Buffer, int nBufferSize); 

	// Summary: 
	//	 utf8 转 unicode
	// Parameters:
	//	 pUTF8		- [IN] 要转换的utf8字符串
	//	 pUnicodeBuffer	- [OUT] 转换后的字符串保存位置
	//	 nBufferSize	- [IN] pUnicodeBuffer的长度(wchar_t个数)
	// Returns:
	//	 转换生成的字符串长(含尾部0)。如果nBufferSize为0且pUnicodeBuffer为NULL,可以得到转换所需的字符串长(也包含尾部0)
	// See also:
	//	 Ansi2Unicode, Unicode2UTF8, Unicode2Ansi
	int UTF82Unicode(const char* pUTF8, wchar_t* pUnicodeBuffer, int nBufferSize);
};
#pragma warning(disable: 4074)
#pragma warning(disable: 4996)

BOOL hcIsValidString(LPCWSTR lpsz, int nLength  = -1);
BOOL hcIsValidString(LPCSTR lpsz, int nLength  = -1);
BOOL hcIsValidAddress(const void* lp, unsigned int nBytes, BOOL bReadWrite = TRUE);

BOOL  hcIsValidString(LPCWSTR lpsz, int nLength /* = -1 */)
{
    
    
	if (lpsz == NULL)
	{
    
    
		return FALSE;
	}

	return ::IsBadStringPtrW(lpsz, nLength) == 0;
}

// As above, but for ANSI strings.

BOOL hcIsValidString(LPCSTR lpsz, int nLength /* = -1 */)
{
    
    
	if (lpsz == NULL)
	{
    
    
		return FALSE;
	}

	return ::IsBadStringPtrA(lpsz, nLength) == 0;
}

BOOL hcIsValidAddress(const void* lp, unsigned int nBytes, BOOL bReadWrite /* = TRUE*/)
{
    
    
	// simple version using Win-32 APIs for pointer validation.
	return (lp != NULL && !IsBadReadPtr(lp, nBytes) &&
		(!bReadWrite || !IsBadWritePtr((LPVOID)lp, nBytes)));
}

int StringUtils::Unicode2Ansi(const wchar_t* pUnicode, char* pAnsiBuffer, int nBufferSize)
{
    
    
	if( (nBufferSize == 0) && (pAnsiBuffer != NULL) )
	{
    
    
		return 0;
	}

	int result = ::WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAnsiBuffer, nBufferSize, NULL, NULL);
	if ((result == 0) && (pAnsiBuffer != NULL) && (nBufferSize > 0))	//ERROR_INSUFFICIENT_BUFFER
	{
    
    
		result = nBufferSize + 1;
	}

	if ((result > 0) && (pAnsiBuffer != NULL))
	{
    
    
		pAnsiBuffer[result-1] = 0;
	}

	return result;
}

int StringUtils::Ansi2Unicode(const char* pAnsi, wchar_t* pUnicodeBuffer, int nBufferSize)
{
    
    
	if( (nBufferSize == 0) && (pUnicodeBuffer != NULL) )
		return 0;

	int result = ::MultiByteToWideChar(CP_ACP, 0, pAnsi, -1, pUnicodeBuffer, nBufferSize);
	if ((result == 0) && (pUnicodeBuffer != NULL) && (nBufferSize > 0))	//ERROR_INSUFFICIENT_BUFFER
		result = nBufferSize + 1;

	if ((result > 0) && (pUnicodeBuffer != NULL))
		pUnicodeBuffer[result-1] = 0;

	return result;
}

int StringUtils::Unicode2UTF8(const wchar_t* pUnicode, char* pUTF8Buffer, int nBufferSize)
{
    
    
	if( (nBufferSize == 0) && (pUTF8Buffer != NULL) )
		return 0;

	int result = WideCharToMultiByte(CP_UTF8, NULL, pUnicode, -1, pUTF8Buffer, nBufferSize, NULL, NULL);
	if ((result == 0) && (pUTF8Buffer != NULL) && (nBufferSize > 0))	//ERROR_INSUFFICIENT_BUFFER
		result = nBufferSize + 1;

	if ((result > 0) && (pUTF8Buffer != NULL))
		pUTF8Buffer[result-1] = 0;

	return result;
}

int StringUtils::UTF82Unicode(const char* pUTF8, wchar_t* pUnicodeBuffer, int nBufferSize)
{
    
    
	if( (nBufferSize == 0) && (pUnicodeBuffer != NULL) )
		return 0;

	int result = MultiByteToWideChar(CP_UTF8, NULL, pUTF8, -1, pUnicodeBuffer, nBufferSize);
	if ((result == 0) && (pUnicodeBuffer != NULL) && (nBufferSize > 0))	//ERROR_INSUFFICIENT_BUFFER
		result = nBufferSize + 1;

	if ((result > 0) && (pUnicodeBuffer != NULL))
		pUnicodeBuffer[result-1] = 0;

	return result;
}

使用方式:

int nLen = HCStringUtils::Unicode2Ansi(szStr, NULL, 0);
HCStringUtils::Unicode2Ansi(szStr, pStrANSI, nLen);

猜你喜欢

转载自blog.csdn.net/m0_37251750/article/details/120672451