C++字符串编码转换
CStrConvert.h
#pragma once
#include <wtypesbase.h>
#include <string>
#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif
class CStrConvert
{
// 宽字符转多字节字符
static std::string _WStrToMultiStr(UINT CodePage, const std::wstring& str);
// 多字节字符转宽字符
static std::wstring _MultiStrToWStr(UINT CodePage, const std::string& str);
// 宽字符串转换
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);
};
CStrConvert.cpp
#include "CStrConvert.h"
std::string CStrConvert::_WStrToMultiStr(UINT CodePage, const std::wstring& str)
{
std::string strResult;
LPSTR lpMultiByteStr = NULL;
do
{
//计算缓冲区所需的字节大小
int nConverted = 0;
nConverted = ::WideCharToMultiByte(CodePage, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
if (0 == nConverted)
{
break;
}
//分配内存
lpMultiByteStr = (LPSTR)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, nConverted);
if (NULL == lpMultiByteStr)
{
break;
}
//转换
nConverted = ::WideCharToMultiByte(CodePage, 0, str.c_str(), -1, lpMultiByteStr, nConverted, NULL, NULL);
if (0 == nConverted)
{
break;
}
strResult = lpMultiByteStr;
} while (false);
//释放字符串缓冲
if (NULL != lpMultiByteStr)
{
::HeapFree(::GetProcessHeap(), 0, lpMultiByteStr);
lpMultiByteStr = NULL;
}
return strResult;
}
std::wstring CStrConvert::_MultiStrToWStr(UINT CodePage, const std::string& str)
{
std::wstring strResult;
LPWSTR lpWideStr = NULL;
do
{
//计算缓冲区所需的字节大小
int nConverted = 0;
nConverted = ::MultiByteToWideChar(CodePage, 0, str.c_str(), -1, NULL, 0);
if (0 == nConverted)
{
break;
}
//分配缓冲内存
lpWideStr = (LPWSTR)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, nConverted * sizeof(WCHAR));
if (NULL == lpWideStr)
{
break;
}
//转换字符串
nConverted = ::MultiByteToWideChar(CodePage, 0, str.c_str(), -1, lpWideStr, nConverted);
if (0 == nConverted)
{
break;
}
strResult = lpWideStr;
} while (false);
//释放字符串缓冲
if (NULL != lpWideStr)
{
::HeapFree(::GetProcessHeap(), 0, lpWideStr);
lpWideStr = NULL;
}
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
}
原文地址:https://blog.csdn.net/Flame_Cyclone/article/details/136041302
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!