字符编码转换,在MFC下使用没问题
XzmBaseFuncLib.h
#pragma once
class CXzmBaseFuncLib
{
public:
CXzmBaseFuncLib(){
}
~CXzmBaseFuncLib(){
}
/**
* @brief 将给定的GBK字符串转换为UTF-8编码
*
*
* @param pSrc
* @param pDst
*/
static int CharToUtf8(const char* pSrc, char** pDst) throw();
/**
* @brief 将给定的Utf8字符串转换为Char编码
*
*
* @param pSrc
* @param pDst
*/
static int Utf8ToChar(LPCSTR pSrc, char** pDst);
/**
* @brief 将给定的UNICODE字符串转换为Char编码
*
*
* @param strSrc
* @param pDst
*/
static int Us2ToChar(const CString& strSrc, char** pDst,int nCodePage = CP_ACP);
/**
* @brief 将给定的GBK字符串转换为UNICODE编码
*
*
* @param szSrc
* @param pDst
*/
static int CharToUS2(const char* szSrc, WCHAR** pDst,int nCodePage = CP_ACP);
/**
* @brief 将给定的UNICODE字符串转换为UTF-8编码
*
*
* @param strSrc
* @param pDst
*/
static int US2ToUtf8(const CString& strSrc,char** pDst) throw();
/**
* @brief 将给定的Utf8字符串转换为UNICODE编码
*
*
* @param pSrc
* @param pDst
*/
static int Utf8ToUS2(LPCSTR pSrc, WCHAR** pDst) throw();
/**
* @brief GBK编码转换为简体中文
*
*
* @param strSrc
* @return
*/
static CString GBKToGB2312(const CString &strSrc);
/**
* @brief 简体中文转换为GBK编码
*
*
* @param strSrc
* @return
*/
static CString GB2312ToGBK(const CString &strSrc);
/**
* @brief 转换文字
*
*
* @param strSrc
* @return
*/
static CString PercentStringDecode(const CString& strSrc,BOOL bUtf8 = FALSE);
/**
* @brief
*
*
* @param &x
* @return
*/
static BYTE toHex(const BYTE &x);
/**
* @brief 将数字转换为字符串
*
*
* @param i
* @return
*/
static CString NumToStr(LONGLONG i);
/**
* @brief 字符串转换到数字
*
*
* @param strTemp
* @return
*/
static DWORD StrToNum(const CString& strTemp);
/**
* @brief 字符串转换到数字,支持超大数字
*
*
* @param strTemp
* @return
*/
static void StrToNum2(const CString& strTemp,DWORD &dwNum1,DWORD &dwNum2);
/**
* @brief 字节流转换到字符串
*
*
* @param szSrc 字节流源
* @param nLen 字节流长度
* @param szDest 目标字符串
* @return
*/
static void ChangeToChar(byte *szSrc,int nLen,char *szDest);
/**
* @brief 判断路径释放存在
*
*
* @param strPath
* @return
*/
static BOOL IsPathExist(const CString& strPath) throw();
};
#include "stdafx.h"
#include "BaseFuncLib.h"
int CXzmBaseFuncLib::CharToUtf8(const char* pSrc, char** pDst) throw()
{
char* cUTF8=NULL;
WCHAR* wchar=NULL;
int iLength=0;
iLength=MultiByteToWideChar(CP_ACP,0,pSrc,-1,NULL,0);
if(!iLength)
return iLength;
iLength++;
wchar=new WCHAR[iLength];
if(wchar==NULL)
return 0;
::memset(wchar,0,iLength*sizeof(WCHAR));
::MultiByteToWideChar(CP_ACP,0,pSrc,-1,wchar,iLength-1);
iLength=::WideCharToMultiByte(CP_UTF8,0,wchar,-1,NULL,0,NULL,NULL);
if(!iLength)
{
delete []wchar;
wchar=NULL;
return iLength;
}
iLength++;
cUTF8=new char[iLength];
if(cUTF8!=NULL)
{
::memset(cUTF8,0,iLength*sizeof(char));
iLength=::WideCharToMultiByte(CP_UTF8,0,wchar,-1,cUTF8,iLength-1,NULL,NULL);
*pDst = cUTF8;
}
else
iLength = 0;
delete []wchar;
wchar=NULL;
return iLength;
}
int CXzmBaseFuncLib::Utf8ToChar(LPCSTR pSrc, char** pDst)
{
WCHAR* strW=NULL;
int iLength=0;
iLength=::MultiByteToWideChar(CP_UTF8,0,pSrc,-1,NULL,0);
if(!iLength)
return iLength;
iLength++;
strW = new WCHAR[iLength];
if(strW==NULL)
return 0;
::memset(strW,0,iLength*sizeof(WCHAR));
::MultiByteToWideChar(CP_UTF8,0,pSrc,-1,strW,iLength-1);
iLength=::WideCharToMultiByte(CP_ACP,0,strW,-1,NULL,0,NULL,NULL);
if(!iLength)
{
delete []strW;
strW=NULL;
return 0;
}
iLength++;
char *strA=NULL;
strA=new char[iLength];
if(strA!=NULL)
{
::memset(strA,0,iLength*sizeof(char));
iLength = ::WideCharToMultiByte(CP_ACP,0,strW,-1,strA,iLength-1,NULL,NULL);
*pDst=strA;
}
else
iLength = 0;
delete []strW;
strW=NULL;
return iLength;
}
int CXzmBaseFuncLib::Us2ToChar(const CString& strSrc, char** pDst,int nCodePage /*= CP_ACP*/)
{
int iLength=::WideCharToMultiByte(nCodePage,0,strSrc,-1,NULL,0,NULL,NULL);
if(!iLength)
return iLength;
iLength++;
char *strA=NULL;
strA= new char[iLength];
if(strA==NULL)
return 0;
::memset(strA,0,iLength*sizeof(char));
int iLength1 = 0;
if(nCodePage)
iLength1 = ::WideCharToMultiByte(nCodePage,WC_COMPOSITECHECK,strSrc,-1,strA,iLength-1,NULL,NULL);
else
iLength1 = ::WideCharToMultiByte(nCodePage,0,strSrc,-1,strA,iLength-1,NULL,NULL);
*pDst=strA;
return iLength1;
}
int CXzmBaseFuncLib::CharToUS2(const char* szSrc, WCHAR** pDst,int nCodePage /*= CP_ACP*/)
{
WCHAR* strW=NULL;
int iLength=0;
iLength=MultiByteToWideChar(nCodePage,0,szSrc,-1,NULL,0);
if(!iLength)
return iLength;
iLength++;
strW=new WCHAR[iLength];
if(strW==NULL)
return 0;
::memset(strW,0,iLength*sizeof(WCHAR));
int iLength1 = 0;
if(nCodePage)
iLength1 = ::MultiByteToWideChar(nCodePage,MB_COMPOSITE,szSrc,-1,strW,iLength-1);
else
iLength1 = ::MultiByteToWideChar(nCodePage,0,szSrc,-1,strW,iLength-1);
*pDst=strW;
return iLength1;
}
int CXzmBaseFuncLib::US2ToUtf8(const CString& strSrc,char** pDst) throw()
{
char* cUTF8=NULL;
int iLength=0;
iLength=::WideCharToMultiByte(CP_UTF8,0,strSrc,-1,NULL,0,NULL,NULL);
if(!iLength)
return iLength;
iLength++;
ATLTRY(cUTF8=new char[iLength]);
if(cUTF8!=NULL)
{
::memset(cUTF8,0,iLength*sizeof(char));
iLength=::WideCharToMultiByte(CP_UTF8,0,strSrc,-1,cUTF8,iLength-1,NULL,NULL);
*pDst = cUTF8;
}
else
iLength = 0;
return iLength;
}
int CXzmBaseFuncLib::Utf8ToUS2(LPCSTR pSrc, WCHAR** pDst) throw()
{
WCHAR *strW = NULL;
int iLength = ::MultiByteToWideChar(CP_UTF8,0,pSrc,-1,NULL,0);
if(!iLength)
return 0;
iLength++;
ATLTRY(strW = new WCHAR[iLength]);
if(NULL == strW)
return 0;
::memset(strW,0,iLength*sizeof(WCHAR));
int iLength1=::MultiByteToWideChar(CP_UTF8,0,pSrc,-1,strW,iLength-1);
*pDst=strW;
return iLength1;
}
CString CXzmBaseFuncLib::GBKToGB2312(const CString &strSrc)
{
DWORD dwErrCode = 0;
CString strDest(_T(""));
DWORD wLanguageID = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_BIG5);
int nLen = ::LCMapString(wLanguageID,LCMAP_SIMPLIFIED_CHINESE,strSrc,-1,NULL,0);
WCHAR *szDest = NULL;
if(nLen)
{
nLen++;
szDest = new WCHAR[nLen];
}
else
dwErrCode = ::GetLastError();
if(NULL == szDest)
return strDest;
memset(szDest,0,sizeof(WCHAR)*nLen);
nLen = ::LCMapString(wLanguageID,LCMAP_SIMPLIFIED_CHINESE,strSrc,-1,szDest,nLen);
if(NULL != szDest)
{
strDest = szDest;
delete []szDest;
szDest = NULL;
}
return strDest;
}
CString CXzmBaseFuncLib::GB2312ToGBK(const CString &strSrc)
{
DWORD dwErrCode = 0;
CString strDest(_T(""));
DWORD wLanguageID = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);
int nLen = ::LCMapString(wLanguageID,LCMAP_TRADITIONAL_CHINESE,strSrc,-1,NULL,0);
WCHAR *szDest = NULL;
if(nLen)
{
nLen++;
szDest = new WCHAR[nLen];
}
else
dwErrCode = ::GetLastError();
if(NULL == szDest)
return strDest;
memset(szDest,0,sizeof(WCHAR)*nLen);
nLen = ::LCMapString(wLanguageID,LCMAP_TRADITIONAL_CHINESE,strSrc,-1,szDest,nLen);
if(NULL != szDest)
{
strDest = szDest;
delete []szDest;
szDest = NULL;
}
return strDest;
}
CString CXzmBaseFuncLib::PercentStringDecode(const CString& strSrc,BOOL bUtf8 /*= FALSE*/)
{
char *szBuf = NULL;
char cTem[8192] = {};
int nLen = strSrc.GetLength(),nC = 0;
for(int nIndex = 0;nIndex < nLen;)
{
CString strVal(_T(""));
WCHAR wc = strSrc.GetAt(nIndex);
if(wc != _T('%'))
{
cTem[nC] = (char)wc;
nC++;
nIndex++;
continue;
}
nIndex++;
if(nIndex < nLen)
{
strVal += strSrc.GetAt(nIndex);
nIndex++;
}
if(nIndex < nLen)
{
strVal += strSrc.GetAt(nIndex);
nIndex++;
}
CXzmBaseFuncLib::Us2ToChar(strVal,&szBuf);
cTem[nC] = (char )strtoul(szBuf, NULL, 16);
nC++;
delete []szBuf;
szBuf = NULL;
if(nC >= 8192)
break;
}
if(bUtf8)
{
CXzmBaseFuncLib::Utf8ToChar(cTem,&szBuf);
CString strReturn(szBuf);
delete []szBuf;
szBuf = NULL;
return strReturn;
}
else
{
CString strReturn(cTem);
return strReturn;
}
}
BYTE CXzmBaseFuncLib::toHex(const BYTE &x)
{
return x > 9 ? x + 55: x + 48;
}
CString CXzmBaseFuncLib::NumToStr(LONGLONG i)
{
CString strTemp(_T(""));
if (i == 0)
{
strTemp = _T("0");
return strTemp;
}
LONGLONG dwNumber = i;
while (dwNumber)
{
TCHAR Temp = (TCHAR)(dwNumber % 10 + '0');
strTemp.Insert(0,Temp);
dwNumber = dwNumber / 10;
}
return strTemp;
}
DWORD CXzmBaseFuncLib::StrToNum(const CString& strTemp)
{
DWORD dwValue = 0;
if(strTemp.IsEmpty())
return dwValue;
for (int j = 0; j < strTemp.GetLength(); j++)
{
TCHAR c = strTemp[j];
if (c <= '9' && c >= '0')
{
dwValue *= 10;
dwValue = (dwValue + (c - '0')) ;
}
}
return dwValue;
}
void CXzmBaseFuncLib::StrToNum2(const CString& strTemp,DWORD &dwNum1,DWORD &dwNum2)
{
dwNum1 =0;
dwNum2 =0;
if(strTemp.IsEmpty())
return;
DWORD dwValue = 0;
int j=0,nLen = strTemp.GetLength();
for (j = 0; j < nLen; j++)
{
TCHAR c = strTemp[j];
if (c <= '9' && c >= '0')
{
dwValue *= 10;
dwValue = (dwValue + (c - '0')) ;
}
if(nLen-8 == j)
{
dwNum1 = dwValue;
dwValue = 0;
}
}
if(dwValue)
dwNum2 = dwValue;
}
void CXzmBaseFuncLib::ChangeToChar(byte *szSrc,int nLen,char *szDest)
{
if(NULL == szSrc || NULL == szDest)
return;
char tmp[4];
for(int nIndex = 0;nIndex<nLen;nIndex++)
{
memset(tmp,0,4);
_itoa_s(szSrc[nIndex],tmp,4,16);
if(strlen(tmp)<2)
strcat_s(szDest,MAX_PATH,"0");
strcat_s(szDest,MAX_PATH,tmp);
}
}
BOOL CXzmBaseFuncLib::IsPathExist(const CString& strPath) throw()
{
BOOL bRet = FALSE;
if(strPath.IsEmpty())
return bRet;
WIN32_FIND_DATA data;
BOOL bFindDir = FALSE;
CString strFind(strPath);
if (0 == strPath.Right(1).CompareNoCase( _T("\\")))
{
/// 查找目录
bFindDir = TRUE;
strFind+=_T("*.*");
}
/**
* @brief FindFirstFile()
*
* @Function
*
* @param[ _In_ ]
*
* @param[ _Out_ ]
*
* @param[_Out_opt_]
*
* @return 如果函数成功,则得到一个句柄,否则失败将得到值INVALID_HANDLE_VALUE
*
* @Date xzm_@_2017/06/30 13:40:11
*/
HANDLE hFindFile = ::FindFirstFile(strFind,&data);
if(INVALID_HANDLE_VALUE != hFindFile)
{
if(bFindDir)
{
while(INVALID_HANDLE_VALUE != hFindFile)
{
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY )
{
bRet = TRUE;
break;
}
/**
* @brief FindNextFile
*
* @Function
*
* @param[ _In_ ]
*
* @param[ _Out_ ]
*
* @param[_Out_opt_]
*
* @return 如果函数成功,则返回值不为零,lpFindFileData 参数包含有关找到的下一个文件或目录的信息。
* 如果函数失败,则返回值为零,并且lpFindFileData的内容 是不确定的。要获取扩展错误信息,请调用 GetLastError函数。
*
* @Date xzm_@_2017/06/30 13:40:11
*/
if (!FindNextFile(hFindFile,&data))
break;
}
}
else
bRet = TRUE;
::FindClose(hFindFile);
hFindFile = NULL;
}
else
bRet=FALSE;
return bRet;
}