CStrConvert.h
#pragma once
#include <wtypesbase.h>
#include <string>
#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif
class CStrConvert
{
public:
// 宽字符串转换
static std::string WStrToU8Str(const std::wstring& str);
static std::string WStrToAStr(const std::wstring& str);
static _tstring WStrToTStr(const std::wstring& str);
// ANSI字符转换
static std::wstring AStrToWStr(const std::string& str);
static std::string AStrToU8Str(const std::string& str);
static _tstring AStrToTStr(const std::string& str);
// UTF-8字符串转换
static std::wstring U8StrToWStr(const std::string& str);
static std::string U8StrToAStr(const std::string& str);
static _tstring U8StrToTStr(const std::string& str);
// 中立字符串转换
static std::string TStrToAStr(const _tstring& str);
static std::wstring TStrToWStr(const _tstring& str);
static std::string TStrToU8Str(const _tstring& str);
private:
// 宽字符转多字节字符
static std::string _WStrToMultiStr(UINT CodePage, const std::wstring& str);
// 多字节字符转宽字符
static std::wstring _MultiStrToWStr(UINT CodePage, const std::string& str);
};
CStrConvert.cpp
#include "CStrConvert.h"
std::string CStrConvert::_WStrToMultiStr(UINT CodePage, const std::wstring& str)
{
//计算缓冲区所需的字节长度
int cbMultiByte = ::WideCharToMultiByte(CodePage, 0, str.c_str(), -1, NULL, 0, NULL, 0);
std::string strResult(cbMultiByte, 0);
//成功则返回写入到指示的缓冲区的字节数
size_t nConverted = ::WideCharToMultiByte(CodePage, 0, str.c_str(), (int)str.size(), &strResult[0], (int)strResult.size(), NULL, NULL);
//调整内容长度
strResult.resize(nConverted);
return strResult;
}
std::wstring CStrConvert::_MultiStrToWStr(UINT CodePage, const std::string& str)
{
//计算缓冲区所需的字符长度
int cchWideChar = ::MultiByteToWideChar(CodePage, 0, str.c_str(), -1, NULL, 0);
std::wstring strResult(cchWideChar, 0);
//成功则返回写入到指示的缓冲区的字符数
size_t nConverted = ::MultiByteToWideChar(CodePage, 0, str.c_str(), (int)str.size(), &strResult[0], (int)strResult.size());
//调整内容长度
strResult.resize(nConverted);
return strResult;
}
std::string CStrConvert::WStrToAStr(const std::wstring& str)
{
return _WStrToMultiStr(CP_ACP, str);
}
std::string CStrConvert::WStrToU8Str(const std::wstring& str)
{
return _WStrToMultiStr(CP_UTF8, str);
}
_tstring CStrConvert::WStrToTStr(const std::wstring& str)
{
#ifdef _UNICODE
return str;
#else
return _WStrToMultiStr(CP_ACP, str);
#endif
}
std::wstring CStrConvert::AStrToWStr(const std::string& str)
{
return _MultiStrToWStr(CP_ACP, str);
}
std::string CStrConvert::AStrToU8Str(const std::string& str)
{
return WStrToU8Str(AStrToWStr(str));
}
_tstring CStrConvert::AStrToTStr(const std::string& str)
{
#ifdef _UNICODE
return _MultiStrToWStr(CP_ACP, str);
#else
return str;
#endif
}
std::wstring CStrConvert::U8StrToWStr(const std::string& str)
{
return _MultiStrToWStr(CP_UTF8, str);
}
std::string CStrConvert::U8StrToAStr(const std::string& str)
{
return WStrToAStr(U8StrToWStr(str));
}
_tstring CStrConvert::U8StrToTStr(const std::string& str)
{
#ifdef _UNICODE
return _MultiStrToWStr(CP_UTF8, str);
#else
return WStrToAStr(U8StrToWStr(str));
#endif
}
std::string CStrConvert::TStrToAStr(const _tstring& str)
{
#ifdef _UNICODE
return _WStrToMultiStr(CP_ACP, str);
#else
return str;
#endif
}
std::wstring CStrConvert::TStrToWStr(const _tstring& str)
{
#ifdef _UNICODE
return str;
#else
return AStrToWStr(str);
#endif
}
std::string CStrConvert::TStrToU8Str(const _tstring& str)
{
#ifdef _UNICODE
return WStrToU8Str(str);
#else
return WStrToU8Str(AStrToWStr(str));
#endif
}