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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

C++程序员Carea

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

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

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

打赏作者

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

抵扣说明:

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

余额充值