1.UTF8(string)转 wstring(这里的wstring就是unicode也就是utf16)
std::wstring UTF82Wide(const std::string& strUTF8)
{
int nWide = ::MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), strUTF8.size(), NULL, 0);
std::unique_ptr
if (!buffer)
{
return L"";
}
::MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), strUTF8.size(), buffer.get(), nWide);
buffer[nWide] = L'\0';
return buffer.get();
}
2.wstring 转 UTF8(string)
std::string Wide2UTF8(const std::wstring& strWide)
{
int nUTF8 = ::WideCharToMultiByte(CP_UTF8, 0, strWide.c_str(), strWide.size(), NULL, 0, NULL, NULL);
std::unique_ptr
if (!buffer)
{
return "";
}
::WideCharToMultiByte(CP_UTF8, 0, strWide.c_str(), strWide.size(), buffer.get(), nUTF8, NULL, NULL);
buffer[nUTF8] = '\0';
return buffer.get();
}
3.ANSI(GBK)转 wstring
std::wstring Ansi2Wide(const std::string& strAnsi)
{
int nWide = ::MultiByteToWideChar(CP_ACP, 0, strAnsi.c_str(), strAnsi.size(), NULL, 0);
std::unique_ptr
if (!buffer)
{
return L"";
}
::MultiByteToWideChar(CP_ACP, 0, strAnsi.c_str(), strAnsi.size(), buffer.get(), nWide);
buffer[nWide] = L'\0';
return buffer.get();
}
4.wstring 转 ANSI
std::string Wide2Ansi(const std::wstring& strWide)
{
int nAnsi = ::WideCharToMultiByte(CP_ACP, 0, strWide.c_str(), strWide.size(), NULL, 0, NULL, NULL);
std::unique_ptr
if (!buffer)
{
return "";
}
::WideCharToMultiByte(CP_ACP, 0, strWide.c_str(), strWide.size(), buffer.get(), nAnsi, NULL, NULL);
buffer[nAnsi] = '\0';
return buffer.get();
}
5。utf16转utf8
string StrUtil::Utf16ToUtf8(const wstring& str)
{
//含\0
int len = ::WideCharToMultiByte(CP_UTF8, NULL, str.c_str(), -1, NULL, NULL, NULL, NULL);
if (len == 0)
{
return string();
}
char* pDst = new char[len];
::WideCharToMultiByte(CP_UTF8, NULL, str.c_str(), -1, pDst, len, NULL, NULL);
string ret(pDst, len - 1);
delete[] pDst;
return ret;
}
6.utf8转utf16
wstring StrUtil::Utf8ToUtf16(const string& str)
{
int len = ::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), -1, NULL, NULL);
if (len == 0)
{
return wstring();
}
wchar_t* pDst = new wchar_t[len];
::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), -1, pDst, len);
wstring ret(pDst, len - 1);
delete[] pDst;
return ret;
}
我们会发现 Utf8ToUtf16和UTF82Wide其实是一样的,只是实现上有一些不一致。
在我们的项目中一般采用utf8编码,它可以用1-6个字节来对应一个字符编码,可以节省不少的空间,下面给出各种类型字符串转成string(默认utf8编码,用notepad++ utf8编码可以看出正确的中文)
string StrUtil::ConvertToStd(const LPWSTR str)
{
wstring src(str);
return Utf16ToUtf8(src);
}
string StrUtil::ConvertToStd(SOUI::SStringT str)
{
wstring src(str.GetBuffer(0));
return Utf16ToUtf8(src);
}
string StrUtil::ConvertToStd(const CString& str)
{
wstring src(str.GetString());
return Utf16ToUtf8(src);
}
string StrUtil::ConvertToStdEx(SOUI::SStringT str)
{
wstring src(str.GetBuffer(0));
//含\0
int len = ::WideCharToMultiByte(CP_ACP, NULL, src.c_str(), -1, NULL, NULL, NULL, NULL);
if (len == 0)
{
return string();
}
char* pDst = new char[len];
::WideCharToMultiByte(CP_ACP, NULL, src.c_str(), -1, pDst, len, NULL, NULL);
string ret(pDst, len - 1);
delete[] pDst;
return ret;
}
然后再看下utf8转成各种字符串类型:
SOUI::SStringT StrUtil::ConvertToSStr(const string& str)
{
wstring s = Utf8ToUtf16(str);
SOUI::SStringT ret(s.c_str());
return ret;
}
CString StrUtil::ConvertToCStr(const string& str)
{
wstring s = Utf8ToUtf16(str);
return CString(s.c_str());
}
CString StrUtil::WstringConvertToCStr(const wstring& str)
{
return CString(str.c_str());
}
然后再来看看其他类型比如int转string类型
string StrUtil::ToString(bool b)
{
return b ? "true" : "false";
}
string StrUtil::ToString(int i, int base/* = 10*/)
{
std::stringstream ss;
//ss << i;
switch(base) {
case 2: //从2 进制
{
char buf[64];
_itoa_s(i, buf, 64, 2);
//_itoa(l,buf , 2);
ss << buf;
}
break;
case 8: //从8 进制
ss << std::oct << i;
break;
case 16: //从16 进制
//ss << "0x";
//ss << ss.width(sizeof(void*)) << ss.fill('0');
ss << std::hex << i;
break;
case 10: //从10 进制
default:
ss << std::dec << i;
break;
}
return ss.str();
}
string StrUtil::ToString(uint32 i, int base/* = 10*/)
{
std::stringstream ss;
//ss << i;
switch(base) {
case 2: //从2 进制
{
char buf[64];
_itoa_s(i, buf, 64, 2);
//_itoa(l,buf , 2);
ss << buf;
}
break;
case 8: //从8 进制
ss << std::oct << i;
break;
case 16: //从16 进制
//ss << "0x";
//ss << ss.width(sizeof(void*)) << ss.fill('0');
ss << std::hex << i;
break;
case 10: //从10 进制
default:
ss << std::dec << i;
break;
}
return ss.str();
}
string StrUtil::ToString(int64 l, int base/* = 10*/)
{
std::stringstream ss;
//ss << l;
switch(base) {
case 2: //从2 进制
{
char buf[128];
_itoa_s(l, buf, 128, 2);
//_itoa(l,buf , 2);
ss << buf;
}
break;
case 8: //从8 进制
ss << std::oct << l;
break;
case 16: //从16 进制
//ss << "0x";
//ss << ss.width(sizeof(void*)) << ss.fill('0');
ss << std::hex << l;
break;
case 10: //从10 进制
default:
ss << std::dec << l;
break;
}
return ss.str();
}
string StrUtil::ToString(uint64 l, int base/* = 10*/)
{
std::stringstream ss;
//ss << l;
switch(base) {
case 2: //从2 进制
{
char buf[128];
_itoa_s(l, buf, 128, 2);
//_itoa(l,buf , 2);
ss << buf;
}
break;
case 8: //从8 进制
ss << std::oct << l;
break;
case 16: //从16 进制
//ss << "0x";
//ss << ss.width(sizeof(void*)) << ss.fill('0');
ss << std::hex << l;
break;
case 10: //从10 进制
default:
ss << std::dec << l;
break;
}
return ss.str();
}
string StrUtil::ToString(double d)
{
std::stringstream ss;
ss << d;
return ss.str();
}
bool StrUtil::ToBool(const string& b, bool def)
{
CStringA cb = b.c_str();
if (cb.CompareNoCase("true") == 0)
{
return true;
}
else if (cb.CompareNoCase("false") == 0)
{
return false;
}
else
{
return def;
}
}
int StrUtil::ToInt(const string& i, int base/* = 10*/)
{
int ret = 0;
if(i.empty()) return ret;
//std::stringstream ss;
//ss << i;
//ss >> ret;
std::stringstream ss(i);
switch(base) {
case 2: //从2 进制
ret = (int)strtol(i.c_str(), NULL, 2);
break;
case 8: //从8 进制
ss >> std::oct >> ret;
break;
case 16: //从16 进制
ss >> std::hex >> ret;
break;
case 10: //从10 进制
default:
ss >> std::dec >> ret;
break;
}
return ret;
}
uint32 StrUtil::ToUInt(const string& i, int base/* = 10*/)
{
uint32 ret = 0;
if(i.empty()) return ret;
//std::stringstream ss;
//ss << i;
//ss >> ret;
std::stringstream ss(i);
switch(base) {
case 2: //从2 进制
ret = strtoul(i.c_str(), NULL, 2);
break;
case 8: //从8 进制
ss >> std::oct >> ret;
break;
case 16: //从16 进制
ss >> std::hex >> ret;
break;
case 10: //从10 进制
default:
ss >> std::dec >> ret;
break;
}
return ret;
}
int64 StrUtil::ToLong(const string& l, int base/* = 10*/)
{
int64 ret = 0;
if(l.empty()) return ret;
//std::stringstream ss;
//ss << l;
//ss >> ret;
std::stringstream ss(l);
switch(base) {
case 2: //从2 进制
ret = strtoll(l.c_str(), NULL, 2);
break;
case 8: //从8 进制
ss >> std::oct >> ret;
break;
case 16: //从16 进制
ss >> std::hex >> ret;
break;
case 10: //从10 进制
default:
ss >> std::dec >> ret;
break;
}
return ret;
}
uint64 StrUtil::ToULong(const string& l, int base/* = 10*/)
{
uint64 ret = 0;
if(l.empty()) return ret;
//std::stringstream ss;
//ss << l;
//ss >> ret;
std::stringstream ss(l);
switch(base) {
case 2: //从2 进制
ret = strtoull(l.c_str(), NULL, 2);
break;
case 8: //从8 进制
ss >> std::oct >> ret;
break;
case 16: //从16 进制
ss >> std::hex >> ret;
break;
case 10: //从10 进制
default:
ss >> std::dec >> ret;
break;
}
return ret;
}
double StrUtil::ToDouble(const string& d)
{
double ret = 0.0;
if (d.empty()) return ret;
std::stringstream ss;
ss << d;
ss >> ret;
return ret;
}