首页 > 其他分享 >字符集编码转换--MFC

字符集编码转换--MFC

时间:2022-11-10 15:09:52浏览次数:37  
标签:MFC return -- CString 字符集 char int iLength NULL


字符编码转换,在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();
};


XzmBaseFuncLib.cpp

#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;
}



标签:MFC,return,--,CString,字符集,char,int,iLength,NULL
From: https://blog.51cto.com/u_15872025/5841614

相关文章

  • 经常使用的一些配置信息路径
    CustomTabCtrl.hrequiresatlgdix.htobeincludedfirstC:\WTL91_5321_Final\Include;XzmSqlite3WtlApp--$(SolutionDir)bin\$(Configuration)\$(ProjectName)\Sqlit......
  • Sublime Text 3 配置
    一、SublimeText3安装1安装安装包:org\xzm-Web开发必备工具\开发工具\SublimeText3-3.1.1-build.3176.zip一路next就可以,没什么可配置的2注册注册码在安装包中3SublimeTe......
  • 过滤CString字符串中各位是数字,大小写字母,符号,汉字
    UNICODE编码下(万国码、国际码、统一码、单一码,双字节字符集编码)UINTGetCharacterType(CStringstr=_T("")){UINTiType=0;//字符串的类型UINTiLen=0;//......
  • cv2嵌入PyQt显示图像与视频
    importsys,osfromPyQt5importQtCore,QtWidgets,QtGuifromPyQt5.QtWidgetsimport*fromPyQt5.QtGuiimport*importcv2classCV2_PYQT_Window(QDialog):def_......
  • eclipse搭建第一个java web应用
    Eslicpe安装下载地址,https://www.eclipse.org/downloads/tomcat服务器安装下载地址,https://tomcat.apache.org/download-80.cgiDynamicwebproject如果你的软件里没有它,请......
  • 文件操作--设置文件属性、获取文件属性
    1.设置文件属性:SetFileAttributes(文件名,属性值)BOOLWINAPI​​SetFileAttributes​​(_In_ LPCTSTRlpFileName,_In_ DWORD  dwFileAttributes);SetFil......
  • 各种编译环境中如何为C++添加命令行参数(Command-line parameter)
    因恐其内容丢失所以重新编辑到本博文中在实际的编程中,我们经常使用命令行参数。命令行参数的英文是Command-lineparameter或者是argument,下面是wikipedia中关于​​Command......
  • PySide 给予开源的node简单功能开发的交互式node功能及操作
    1,添加界面2,添加操作面板3,添加箭头4,添加双击添加节点功能5,添加连接节点自动添加节点接口数,断开删除接口。6,输出节点信息,并通过读取可以自动创建节点7,删除节点端口后恢......
  • VS各种工程文件说明
    一、sln文件.sln(Solution)解决方案文件,表示一个项目组,他通常包含一个项目中所有的工程文件信息。二、suo文件suo(SolutionUserOptions)解决方案用......
  • Source code manager common
    ​​TFS2010安装教程​​​​TFS2010官方下载​​​​TFS2010下载及其说明​​​​VS2010与TFS2010下载地址​​​​如何添加TFS团队成员权限​​​​TFS2010+sharepoint3安......