C++ 字符串各种类型转换的函数封装 【宝藏博客】

目录

char 与 wchar_t

 Ansi 转 Unicode

Unicode 转 Ansi

Ansi转Utf8

Utf8转Ansi

 Unicode转Utf8

Utf8转Unicode

Ansi转Unicode 

Unicode转Ansi

Ansi转Utf8

 Utf8转Ansi

Unicode转Utf8 

Utf8转Unicode

string 与 wstring 

Ansi转Unicode

Unicode转Ansi

Ansi转Utf8 

 Utf8转Ansi

Unicode转Utf8 

Utf8转Unicode


char 与 wchar_t

 Ansi 转 Unicode

wchar_t* AnsiToUnicode(const char* lpszStr)
{
	wchar_t* lpUnicode;
	int nLen;

	if (NULL == lpszStr)
		return NULL;

	nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszStr, -1, NULL, 0);
	if (0 == nLen)
		return NULL;

	lpUnicode = new wchar_t[nLen + 1];
	if (NULL == lpUnicode)
		return NULL;

	memset(lpUnicode, 0, sizeof(wchar_t)* (nLen + 1));
	nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszStr, -1, lpUnicode, nLen);
	if (0 == nLen)
	{
		delete[]lpUnicode;
		return NULL;
	}

	return lpUnicode;
}

 Unicode 转 Ansi

char* UnicodeToAnsi(const wchar_t* lpszStr)
{
	char* lpAnsi;
	int nLen;

	if (NULL == lpszStr)
		return NULL;

	nLen = ::WideCharToMultiByte(CP_ACP, 0, lpszStr, -1, NULL, 0, NULL, NULL);
	if (0 == nLen)
		return NULL;

	lpAnsi = new char[nLen + 1];
	if (NULL == lpAnsi)
		return NULL;

	memset(lpAnsi, 0, nLen + 1);
	nLen = ::WideCharToMultiByte(CP_ACP, 0, lpszStr, -1, lpAnsi, nLen, NULL, NULL);
	if (0 == nLen)
	{
		delete[]lpAnsi;
		return NULL;
	}

	return lpAnsi;
}

Ansi转Utf8

char* AnsiToUtf8(const char* lpszStr)
{
	wchar_t* lpUnicode;
	char* lpUtf8;
	int nLen;

	if (NULL == lpszStr)
		return NULL;

	nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszStr, -1, NULL, NULL);
	if (0 == nLen)
		return NULL;

	lpUnicode = new wchar_t[nLen + 1];
	if (NULL == lpUnicode)
		return NULL;

	memset(lpUnicode, 0, sizeof(wchar_t)* (nLen + 1));
	nLen = ::MultiByteToWideChar(CP_ACP, 0, lpszStr, -1, lpUnicode, nLen);
	if (0 == nLen)
	{
		delete[]lpUnicode;
		return NULL;
	}

	nLen = ::WideCharToMultiByte(CP_UTF8, 0, lpUnicode, -1, NULL, 0, NULL, NULL);
	if (0 == nLen)
	{
		delete[]lpUnicode;
		return NULL;
	}

	lpUtf8 = new char[nLen + 1];
	if (NULL == lpUtf8)
	{
		delete[]lpUnicode;
		return NULL;
	}

	memset(lpUtf8, 0, nLen + 1);
	nLen = ::WideCharToMultiByte(CP_UTF8, 0, lpUnicode, -1, lpUtf8, nLen, NULL, NULL);
	if (0 == nLen)
	{
		delete[]lpUnicode;
		delete[]lpUtf8;
		return NULL;
	}

	delete[]lpUnicode;

	return lpUtf8;
}

Utf8转Ansi

char* Utf8ToAnsi(const char* lpszStr)
{
	wchar_t* lpUnicode;
	char* lpAnsi;
	int nLen;

	if (NULL == lpszStr)
		return NULL;

	nLen = ::MultiByteToWideChar(CP_UTF8, 0, lpszStr, -1, NULL, NULL);
	if (0 == nLen)
		return NULL;

	lpUnicode = new wchar_t[nLen + 1];
	if (NULL == lpUnicode)
		return NULL;

	memset(lpUnicode, 0, sizeof(wchar_t)* (nLen + 1));
	nLen = ::MultiByteToWideChar(CP_UTF8, 0, lpszStr, -1, lpUnicode, nLen);
	if (0 == nLen)
	{
		delete[]lpUnicode;
		return NULL;
	}

	nLen = ::WideCharToMultiByte(CP_ACP, 0, lpUnicode, -1, NULL, 0, NULL, NULL);
	if (0 == nLen)
	{
		delete[]lpUnicode;
		return NULL;
	}

	lpAnsi = new char[nLen + 1];
	if (NULL == lpAnsi)
	{
		delete[]lpUnicode;
		return NULL;
	}

	memset(lpAnsi, 0, nLen + 1);
	nLen = ::WideCharToMultiByte(CP_ACP, 0, lpUnicode, -1, lpAnsi, nLen, NULL, NULL);
	if (0 == nLen)
	{
		delete[]lpUnicode;
		delete[]lpAnsi;
		return NULL;
	}

	delete[]lpUnicode;

	return lpAnsi;
}

 Unicode转Utf8

char* UnicodeToUtf8(const wchar_t* lpszStr)
{
	char* lpUtf8;
	int nLen;

	if (NULL == lpszStr)
		return NULL;

	nLen = ::WideCharToMultiByte(CP_UTF8, 0, lpszStr, -1, NULL, 0, NULL, NULL);
	if (0 == nLen)
		return NULL;

	lpUtf8 = new char[nLen + 1];
	if (NULL == lpUtf8)
		return NULL;

	memset(lpUtf8, 0, nLen + 1);
	nLen = ::WideCharToMultiByte(CP_UTF8, 0, lpszStr, -1, lpUtf8, nLen, NULL, NULL);
	if (0 == nLen)
	{
		delete[]lpUtf8;
		return NULL;
	}

	return lpUtf8;
}

Utf8转Unicode

wchar_t* Utf8ToUnicode(const char* lpszStr)
{
	wchar_t* lpUnicode;
	int nLen;

	if (NULL == lpszStr)
		return NULL;

	nLen = ::MultiByteToWideChar(CP_UTF8, 0, lpszStr, -1, NULL, 0);
	if (0 == nLen)
		return NULL;

	lpUnicode = new wchar_t[nLen + 1];
	if (NULL == lpUnicode)
		return NULL;

	memset(lpUnicode, 0, sizeof(wchar_t)* (nLen + 1));
	nLen = ::MultiByteToWideChar(CP_UTF8, 0, lpszStr, -1, lpUnicode, nLen);
	if (0 == nLen)
	{
		delete[]lpUnicode;
		return NULL;
	}

	return lpUnicode;
}

Ansi转Unicode 

bool AnsiToUnicode(const char* lpszAnsi, wchar_t* lpszUnicode, int nLen)
{
	int nRet = ::MultiByteToWideChar(CP_ACP, 0, lpszAnsi, -1, lpszUnicode, nLen);
	return (0 == nRet) ? FALSE : TRUE;
}

Unicode转Ansi

bool UnicodeToAnsi(const wchar_t* lpszUnicode, char* lpszAnsi, int nLen)
{
	int nRet = ::WideCharToMultiByte(CP_ACP, 0, lpszUnicode, -1, lpszAnsi, nLen, NULL, NULL);
	return (0 == nRet) ? FALSE : TRUE;
}

Ansi转Utf8

bool AnsiToUtf8(const char* lpszAnsi, char* lpszUtf8, int nLen)
{
	wchar_t* lpszUnicode = AnsiToUnicode(lpszAnsi);
	if (NULL == lpszUnicode)
		return FALSE;

	int nRet = UnicodeToUtf8(lpszUnicode, lpszUtf8, nLen);

	delete[]lpszUnicode;

	return (0 == nRet) ? FALSE : TRUE;
}

 Utf8转Ansi

bool Utf8ToAnsi(const char* lpszUtf8, char* lpszAnsi, int nLen)
{
	wchar_t* lpszUnicode = Utf8ToUnicode(lpszUtf8);
	if (NULL == lpszUnicode)
		return FALSE;

	int nRet = UnicodeToAnsi(lpszUnicode, lpszAnsi, nLen);

	delete[]lpszUnicode;

	return (0 == nRet) ? FALSE : TRUE;
}

Unicode转Utf8 

bool UnicodeToUtf8(const wchar_t* lpszUnicode, char* lpszUtf8, int nLen)
{
	int nRet = ::WideCharToMultiByte(CP_UTF8, 0, lpszUnicode, -1, lpszUtf8, nLen, NULL, NULL);
	return (0 == nRet) ? FALSE : TRUE;
}

Utf8转Unicode

bool Utf8ToUnicode(const char* lpszUtf8, wchar_t* lpszUnicode, int nLen)
{
	int nRet = ::MultiByteToWideChar(CP_UTF8, 0, lpszUtf8, -1, lpszUnicode, nLen);
	return (0 == nRet) ? FALSE : TRUE;
}

string 与 wstring 

Ansi转Unicode

std::wstring AnsiToUnicode(const std::string& strAnsi)
{
	std::wstring strUnicode;

	wchar_t* lpszUnicode = AnsiToUnicode(strAnsi.c_str());
	if (lpszUnicode != NULL)
	{
		strUnicode = lpszUnicode;
		delete[]lpszUnicode;
	}

	return strUnicode;
}

Unicode转Ansi

std::string UnicodeToAnsi(const std::wstring& strUnicode)
{
	std::string strAnsi;

	char* lpszAnsi = UnicodeToAnsi(strUnicode.c_str());
	if (lpszAnsi != NULL)
	{
		strAnsi = lpszAnsi;
		delete[]lpszAnsi;
	}

	return strAnsi;
}

Ansi转Utf8 

std::string AnsiToUtf8(const std::string& strAnsi)
{
	std::string strUtf8;

	char* lpszUtf8 = AnsiToUtf8(strAnsi.c_str());
	if (lpszUtf8 != NULL)
	{
		strUtf8 = lpszUtf8;
		delete[]lpszUtf8;
	}

	return strUtf8;
}

 Utf8转Ansi

std::string Utf8ToAnsi(const std::string& strUtf8)
{
	std::string strAnsi;

	char* lpszAnsi = Utf8ToAnsi(strUtf8.c_str());
	if (lpszAnsi != NULL)
	{
		strAnsi = lpszAnsi;
		delete[]lpszAnsi;
	}

	return strAnsi;
}

Unicode转Utf8 

std::string UnicodeToUtf8(const std::wstring& strUnicode)
{
	std::string strUtf8;

	char* lpszUtf8 = UnicodeToUtf8(strUnicode.c_str());
	if (lpszUtf8 != NULL)
	{
		strUtf8 = lpszUtf8;
		delete[]lpszUtf8;
	}

	return strUtf8;
}

Utf8转Unicode

std::wstring Utf8ToUnicode(const std::string& strUtf8)
{
	std::wstring strUnicode;

	wchar_t* lpszUnicode = Utf8ToUnicode(strUtf8.c_str());
	if (lpszUnicode != NULL)
	{
		strUnicode = lpszUnicode;
		delete[]lpszUnicode;
	}

	return strUnicode;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员陈子青

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值