StringHelper完成字符编码转换Unicode UTF8 ANSI

说明:

1 这个版本至少我经历的3家公司都是这么用的;

2 new失败不处理,我擦,new都失败了你还想搞啥?起死回生?写日志?拉倒吧,内存泄漏要用内存管理那一套方法(内存管理自动化:也就是不要去管理,交给STL和智能指针就完了)解决;

3 这里主要考虑的是Windows环境的编程,都#include<Windows.h>了就别整什么跨平台了。跨平台用QT,不用QT就别自己跨。

头文件

#pragma once

#ifdef STRINGHELPER_EXPORT     
#define STRINGHELPER_API __declspec(dllexport)      
#else     
#define STRINGHELPER_API __declspec(dllimport)      
#endif 

/*****************************************
作者: QQ3508551694
 
创建日期:2019-08-18
作用:
修改日期:
*****************************************/

#include <string>
#include <sstream>
#include <list>

class STRINGHELPER_API StringHelper
{
public:
	static std::string UnicodeToANSI(const std::wstring& from);
	static std::string UnicodeToUTF8(const std::wstring& from);

	static std::wstring ANSIToUnicode(const std::string& from);
	static std::string ANSIToUTF8(const std::string& from);

	static std::wstring UTF8ToUnicode(const std::string& from);
	static std::string UTF8ToANSI(const std::string& from);
};

源文件

#include "StringHelper/StringHelper.h"

#include <list>
#include <algorithm>
#include <Windows.h>

std::string StringHelper::UnicodeToANSI(const std::wstring& from)
{
	if (from.empty())
	{
		return std::string();
	}

	size_t countByte = 0;
	char *charOut = nullptr;
	if (countByte = ::WideCharToMultiByte(CP_ACP, 0, from.c_str(), from.size(), NULL, 0, 0, 0))
	{
		charOut = new char[countByte + 1];
		memset(charOut, 0, countByte + 1);
		if (!::WideCharToMultiByte(CP_ACP, 0, from.c_str(), from.size(), charOut, countByte, 0, 0))
		{
			delete[] charOut;//clean up if failed;  
			return std::string();
		}
	}
	else
	{
		return std::string();
	}
	std::string strRet(charOut);
	delete[] charOut;//clean up if success;

	return std::move(strRet);
}

std::wstring StringHelper::ANSIToUnicode(const std::string& from)
{
	if (from.empty())
	{
		return std::wstring();
	}

	size_t unicodeLen = 0;
	wchar_t *  pUnicode = nullptr;
	if (unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, from.c_str(), -1, NULL, 0))
	{
		pUnicode = new wchar_t[unicodeLen + 1];
		memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
		if (!::MultiByteToWideChar(CP_ACP, 0, from.c_str(), -1, (LPWSTR)pUnicode, unicodeLen))
		{
			delete[] pUnicode;//clean up if failed;  
			return std::wstring();
		}
	}
	else
	{
		return std::wstring();
	}
	std::wstring strRet(pUnicode);
	delete[] pUnicode;//clean up if success;

	return std::move(strRet);
}

std::string StringHelper::UTF8ToANSI(const std::string& from)
{
	return std::move(UnicodeToANSI(UTF8ToUnicode(from)));
}
std::string StringHelper::ANSIToUTF8(const std::string& from)
{
	return std::move(UnicodeToUTF8(ANSIToUnicode(from)));
}

std::wstring StringHelper::UTF8ToUnicode(const std::string& from)
{
	if (from.empty())
	{
		return std::wstring();
	}

	size_t unicodeLen = 0;
	wchar_t *  pUnicode = nullptr;
	if (unicodeLen = ::MultiByteToWideChar(CP_UTF8, 0, from.c_str(), -1, NULL, 0))
	{
		pUnicode = new  wchar_t[unicodeLen + 1];
		memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
		if (!::MultiByteToWideChar(CP_UTF8, 0, from.c_str(), -1, (LPWSTR)pUnicode, unicodeLen))
		{
			delete[] pUnicode;//clean up if failed;  
			return std::wstring();
		}
	}
	else
	{
		return std::wstring();
	}
	std::wstring  tmp(pUnicode);
	delete[]  pUnicode;
	return  std::move(tmp);
}

std::string StringHelper::UnicodeToUTF8(const std::wstring& from)
{
	if (from.empty())
	{
		return std::string();  
	}

	size_t countByte = 0;
	char *charOut = nullptr;
	if (countByte = ::WideCharToMultiByte(CP_UTF8, 0, from.c_str(), from.size(), NULL, 0, 0, 0))
	{
		charOut = new char[countByte + 1];
		memset(charOut, 0, countByte + 1);
		if (!::WideCharToMultiByte(CP_UTF8, 0, from.c_str(), from.size(), charOut, countByte, 0, 0))
		{
			delete[]charOut;//clean up if failed;  
			return std::string();
		}
	}
	else
	{
		return std::string();
	}
	std::string strRet(charOut);
	delete[] charOut;//clean up if success;

	return std::move(strRet);
}
发布了506 篇原创文章 · 获赞 199 · 访问量 117万+

猜你喜欢

转载自blog.csdn.net/ClamReason/article/details/104282395