栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033093.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号