基础类型的转换方法(持续更新...)

这篇博客详细介绍了在C++中如何进行不同字符串类型如CString、string、wstring之间的转换,以及如何在不同字符集(如GBK到UTF8)间转换。此外,还提供了一个名为CCommonfunc的类,包含了字符串格式化、修剪和替换等实用函数。
摘要由CSDN通过智能技术生成

目录

CString转long

CString转int

CString转string

string转CString

CString转wstring 

wstring转CString

将当前字符集(我目前是简体中文GB2312)的CString转换为utf8字符集的string

string格式化类(有一些格式转换的函数)

头文件(Commonfunc.h)

源文件(Commonfunc.cpp)


格式转换函数(MultiByteToWideChar()、WideCharToMultiByte())说明链接:https://learn.microsoft.com/zh-cn/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte

CString转long

CString str("123");
long nNumber = atol(str);

CString转int

CString str("123");
int nNumber = atoi(str);

CString转string

string CString2String(CString cstr)
{
	string strTemp = CT2A(cstr);
	return strTemp;
}

string转CString

CString String2CString(string str)
{
	return CString(str.c_str());
}

CString转wstring 

std::wstring CStringToWString(const CString& s)
{
	std::string s2;
	s2 = std::string((LPCTSTR)s);
	size_t i;
	std::string curLocale = setlocale(LC_ALL, NULL);
	setlocale(LC_ALL, "chs");
	const char* _source = s2.c_str();
	size_t _dsize = s2.size() + 1;
	wchar_t* _dest = new wchar_t[_dsize];
	wmemset(_dest, 0x0, _dsize);
	mbstowcs_s(&i, _dest, _dsize, _source, _dsize);
	std::wstring wtext = _dest;
	delete[] _dest;
	setlocale(LC_ALL, curLocale.c_str());
	return wtext;
}

wstring转CString

CString WStringToCString(const std::wstring& s)
{
	std::string strLocale = setlocale(LC_ALL, "");
	const wchar_t* wchSrc = s.c_str();
	size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
	char* chDest = new char[nDestSize];
	memset(chDest, 0, nDestSize);
	wcstombs(chDest, wchSrc, nDestSize);
	std::string strResult = chDest;
	delete[]chDest;
	setlocale(LC_ALL, strLocale.c_str());
	return strResult.c_str();
}

将当前字符集(我目前是简体中文GB2312)的CString转换为utf8字符集的string

std::string ConvertCStringToUTF8(const CString& cstr)
{
	string str = CString2String(cstr);

	int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
	wchar_t* pwBuf = new wchar_t[nwLen + 1]{0};
	::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);

	int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
	char* pBuf = new char[(nLen + 1) * sizeof(wchar_t)]{ 0 };
	::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

	std::string retStr(pBuf);

	delete[]pwBuf;
	pwBuf = NULL;
	delete[]pBuf;
	pBuf = NULL;

	return retStr;
}

string格式化类(有一些格式转换的函数)

头文件(Commonfunc.h)
#pragma once
#include <string>

class CCommonfunc
{
public:
	CCommonfunc(void);
	~CCommonfunc(void);

	static std::string& string_format(std::string & _str, const char * _Format, ...);
	static std::string& string_trimleft(std::string& str,char ch = ' ');
	static std::string& string_trimright(std::string& str,char ch = ' ');
	static std::string& string_trimleftright(std::string& str,char ch = ' ');
	static std::string& string_delallmark(std::string& str,std::string strmark);
	static std::string& replace_str(std::string& str, const std::string& to_replaced, const std::string& newchars);

	static std::wstring AnsiToUniCode(std::string strSrc);
	static std::string UniCodeToAnsi(std::wstring strSrc);
	static std::string UniCodeToUtf8(std::wstring strSrc);
	static std::wstring Utf8ToUniCode(std::string strSrc);
	static std::string AnsiToUtf8(std::string strSrc);
	static std::string Utf8ToAnsi(std::string strSrc);

};

源文件(Commonfunc.cpp)
#include "pch.h"
#include "Commonfunc.h"


CCommonfunc::CCommonfunc(void)
{

}


CCommonfunc::~CCommonfunc(void)
{

}

std::string& CCommonfunc::string_format(std::string & _str, const char * _Format, ...) 
{
	std::string tmp;
	va_list marker = NULL;
	va_start(marker, _Format);

	size_t num_of_chars = _vscprintf(_Format, marker);

	if (num_of_chars >= tmp.capacity()) 
	{
		tmp.resize(num_of_chars + 1);
	}

	vsprintf_s((char *)tmp.data(), tmp.capacity(), _Format, marker);

	va_end(marker);

	_str = tmp.c_str();
	return _str;
}

std::string& CCommonfunc::string_trimleft(std::string& str,char ch)
{
	str = str.substr(str.find_first_not_of(ch),std::string::npos);
	return str;
}

std::string& CCommonfunc::string_trimright(std::string& str,char ch)
{
	str = str.substr(0,str.find_last_not_of(ch) + 1);
	return str;
}

std::string& CCommonfunc::string_trimleftright(std::string& str,char ch)
{
	string_trimleft(str,ch);
	string_trimright(str,ch);

	return str;
}

std::string& CCommonfunc::string_delallmark(std::string& str,std::string strmark)
{
	size_t nSize = strmark.size();

	while(1)
	{
		size_t pos = str.find(strmark);
		if(pos == std::string::npos)
		{
			break;
		}

		str.erase(pos, nSize);
	}

	return str;
}

std::string& CCommonfunc::replace_str(std::string& str, const std::string& to_replaced, const std::string& newchars)
{
	for(std::string::size_type pos(0); pos != std::string::npos; pos += newchars.length())
	{
		pos = str.find(to_replaced,pos);
		if(pos!=std::string::npos)
			str.replace(pos,to_replaced.length(),newchars);
		else
			break;
	}
	return   str;
}

std::wstring CCommonfunc::AnsiToUniCode(std::string strSrc)
{
	//预转换,得到所需空间的大小
	int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, strSrc.c_str(), strSrc.size(), NULL, 0);
	//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
	wchar_t* wszString = new wchar_t[wcsLen + 1];
	//转换
	::MultiByteToWideChar(CP_ACP, NULL, strSrc.c_str(), strSrc.size(), wszString, wcsLen);
	//最后加上'\0'
	wszString[wcsLen] = '\0';

	std::wstring wstrTmp = wszString;
	delete []wszString;
	return wstrTmp;
}
std::string CCommonfunc::UniCodeToAnsi(std::wstring strSrc)
{
	int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, strSrc.c_str(), strSrc.size(), NULL, 0, NULL, NULL);
	//同上,分配空间要给'\0'留个空间
	char* szAnsi = new char[ansiLen + 1];
	//转换
	//unicode版对应的strlen是wcslen
	::WideCharToMultiByte(CP_ACP, NULL, strSrc.c_str(), strSrc.size(), szAnsi, ansiLen, NULL, NULL);
	//最后加上'\0'
	szAnsi[ansiLen] = '\0';

	std::string strTmp = szAnsi;
	delete []szAnsi;
	return strTmp;
}
std::string CCommonfunc::UniCodeToUtf8(std::wstring strSrc)
{
	int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, strSrc.c_str(), strSrc.size(), NULL, 0, NULL, NULL);
	//同上,分配空间要给'\0'留个空间
	//UTF8虽然是Unicode的压缩形式,但也是多字节字符串,所以可以以char的形式保存
	char* szU8 = new char[u8Len + 1];
	//转换
	//unicode版对应的strlen是wcslen
	::WideCharToMultiByte(CP_UTF8, NULL, strSrc.c_str(), strSrc.size(), szU8, u8Len, NULL, NULL);
	//最后加上'\0'
	szU8[u8Len] = '\0';

	std::string strTmp = szU8;
	delete []szU8;
	return strTmp;
}
std::wstring CCommonfunc::Utf8ToUniCode(std::string strSrc)
{
	//UTF8 to Unicode	
	//预转换,得到所需空间的大小
	int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, strSrc.c_str(), strSrc.size(), NULL, 0);
	//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
	wchar_t* wszString = new wchar_t[wcsLen + 1];
	//转换
	::MultiByteToWideChar(CP_UTF8, NULL, strSrc.c_str(), strSrc.size(), wszString, wcsLen);
	//最后加上'\0'
	wszString[wcsLen] = '\0';

	std::wstring wstrTmp = wszString;
	delete []wszString;
	return wstrTmp;
}

std::string CCommonfunc::AnsiToUtf8(std::string strSrc)
{
	return UniCodeToUtf8(AnsiToUniCode(strSrc));
}

std::string CCommonfunc::Utf8ToAnsi(std::string strSrc)
{
	return UniCodeToAnsi(Utf8ToUniCode(strSrc));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值