目录
将当前字符集(我目前是简体中文GB2312)的CString转换为utf8字符集的string
格式转换函数(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));
}