字符串转换,各种编码格式各种转

Windows平台编码转化,任何乱码问题,统统搞定,我一直使用这个:

头文件:

#pragma once
#include "stdafx.h"
#include <string>


//字符串各种转
class CCharCvt
{
public:
CCharCvt();
~CCharCvt();


public:
static  bool AnsiToUnicode(const char* szAnsi, std::wstring& szUnicode);
static bool UnicodeToAnsi(const wchar_t* szUnicode, std::string& szAnsi);
static bool UnicodeToUTF8(const wchar_t* szUnicode, std::string& szUTF8);
static bool AnsiToUTF8(const char* szAnsi, std::string& szUTF8);
static bool UTF8ToUnicode(const char* szUTF8, std::wstring& strUnicode);
static bool UTF8ToAnsi(const char* szUTF8, std::string& strAnsi);
};

源文件:

#include "CCharCvt.h"
#include <memory>


using namespace std;


CCharCvt::CCharCvt(void)
{
}




CCharCvt::~CCharCvt(void)
{
}


/*-------------------------------------------------------------------------
 time : 2015年5月8日   16时40分
 func : ANSI转Unicode
 para : 
 ret  : 
-------------------------------------------------------------------------*/
bool CCharCvt::AnsiToUnicode(const char* szAnsi, wstring& szUnicode)
{
UINT nNeededLen = MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, NULL, 0);


unique_ptr<wchar_t[]> wszTmp(new wchar_t[nNeededLen]);
if(!wszTmp)
{
return false;
}


MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, wszTmp.get(), nNeededLen);
szUnicode = wszTmp.get();


return true;
}


/*-------------------------------------------------------------------------
 time : 2015年5月8日   16时44分
 func : Unicode转ANSI
 para : 
 ret  : 
-------------------------------------------------------------------------*/
bool CCharCvt::UnicodeToAnsi(const wchar_t* szUnicode, string& szAnsi)
{
UINT nNeededLen = WideCharToMultiByte(CP_ACP, 0, szUnicode, -1, NULL, 0, NULL, NULL);


unique_ptr<char[]> szTmp(new char[nNeededLen]);
if(!szTmp)
{
return false;
}

WideCharToMultiByte(CP_ACP, 0, szUnicode, -1, szTmp.get(), nNeededLen, NULL, NULL);
szAnsi = szTmp.get();


return true;
}


/*-------------------------------------------------------------------------
 time : 2015年5月8日   16时44分
 func : Unicode转UTF8
 para : 
 ret  : 
-------------------------------------------------------------------------*/
bool CCharCvt::UnicodeToUTF8(const wchar_t* szUnicode, string& szUTF8)
{
UINT nNeededLen = WideCharToMultiByte(CP_UTF8, 0, szUnicode, -1, NULL, 0, NULL, NULL);


unique_ptr<char[]> szTmp(new char[nNeededLen]);
if(!szTmp)
{
return false;
}


WideCharToMultiByte(CP_UTF8, 0, szUnicode, -1, szTmp.get(), nNeededLen, NULL, NULL);
szUTF8 = szTmp.get();


return true;
}


/*-------------------------------------------------------------------------
 time : 2015年5月8日   16时45分
 func : Ansi转UTF8
 para : 
 ret  : 
-------------------------------------------------------------------------*/
bool CCharCvt::AnsiToUTF8(const char* szAnsi, string& szUTF8)
{
wstring wstrTmp;
if(!AnsiToUnicode(szAnsi, wstrTmp))
{
return false;
}
if(!UnicodeToUTF8(wstrTmp.c_str(), szUTF8))
{
return false;
}


return true;
}


/*-------------------------------------------------------------------------
 time : 2015年5月8日   16时45分
 func : UTF8转Unicode
 para : 
 ret  : 
-------------------------------------------------------------------------*/
bool CCharCvt::UTF8ToUnicode(const char* szUTF8, wstring& strUnicode)
{
UINT nNeededLen = MultiByteToWideChar(CP_UTF8, 0, szUTF8, -1, NULL, 0);


unique_ptr<wchar_t[]> szTmp(new wchar_t[nNeededLen]);
if(!szTmp)
{
return false;
}


MultiByteToWideChar(CP_UTF8, 0, szUTF8, -1, szTmp.get(), nNeededLen);
strUnicode = szTmp.get();


return true;
}


/*-------------------------------------------------------------------------
 time : 2015年5月8日   16时45分
 func : UTF8转Ansi
 para : 
 ret  : 
-------------------------------------------------------------------------*/
bool CCharCvt::UTF8ToAnsi(const char* szUTF8, string& strAnsi)
{
wstring strTmp;
if(!UTF8ToUnicode(szUTF8, strTmp))
{
return false;
}
if(!UnicodeToAnsi(strTmp.c_str(), strAnsi))
{
return false;
}


return true;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值