windows c++中存在各种类型,在实际应用过程中也需要将类型互相转换,故整理了常用类型之间的转换并将之封装成函数,仅供参考,有什么不对的地方,还请指正!
***********************************************************************
1、函数:ToInteger
描述:将CString转换为int
**********************************************************************
int ToInteger(CString csSource)
{
if (csSource .Trim() == _T(""))
{
return -1;
}
int nResult = _ttoi(csSource);
return nResult;
}
***********************************************************************
2、函数:ToString
描述:将int转换为CString
**********************************************************************
bHex表示是否转换为十六进制
CString ToString(int nSource,BOOL bHex)
{
if (bHex)
{
strResult.Format(_T("%02x"),nSource);
}
else
{
strResult.Format(_T("%d"),nSource);
}
return strResult;
}
***********************************************************************
3、函数:ToString
描述:将int转换为CString
注意:delete
**********************************************************************
void ToPChar(LPWSTR pszSource,char* &pcrdest)
{
int nsourceLen=WideCharToMultiByte(CP_ACP,0,pszSource,-1,NULL,0,NULL,NULL);
if (nsourceLen==0)
{
AfxMessageBox(_T("Empty input value!"));
return;
}
pcrdest=new char[nsourceLen+1];
memset(pcrdest,0x00,sizeof(char)*(nsourceLen+1));
::WideCharToMultiByte(CP_ACP,0,pszSource,-1,pcrdest,nsourceLen,NULL,NULL);
}
***********************************************************************
4、函数:无
描述:CString转char *
**********************************************************************
#include <atlconv.h>
USES_CONVERSION;
char *cMac = T2A(csCommunitation);
***********************************************************************
4、函数:无
描述:char *转wchar *
**********************************************************************
①
#include <atlconv.h>
USES_CONVERSION;
char *c = "Hello";
char *cMac = A2W(c);
②
wchar_t ws[100];
swprintf(ws, 100, L"%hs", "ansi string");
const wchar_t *GetWC(const char *c)
{
const size_t cSize = strlen(c)+1;
wchar_t* wc = new wchar_t[cSize];
mbstowcs (wc, c, cSize);
return wc;
}
***********************************************************************
4、函数:无
描述:wchar *转char*
**********************************************************************
①
USES_CONVERSION;
WCHAR* wc = L"Hello World" ;
char* c = W2A(wc);
②
char output[256];
WCHAR* wc = L"Hellow World" ;
sprintf(output, "%ws", wc );
③
#include <comdef.h> // you will need this
const WCHAR* wc = L"Hello World" ;
_bstr_t b(wc);
const char* c = b;
printf("Output: %s\n", c);
***********************************************************************
5、函数:Convert
描述:将指针字符串转换为无符号指针字符串
**********************************************************************
void Convert (const char* szStr,unsigned char *pBuf,int nBufSize)
{
char szTemp[3] = {0};
int nLen = strlen(szStr);
for (int i = 0,j = 0; i < nLen - 1,j < nBufSize; i += 2,j++)
{
szTemp[0] = szStr[i];
szTemp[1] = szStr[i + 1];
pBuf[j] = (unsigned char) strtoul(szTemp,NULL,16);
}
}
***********************************************************************
6、函数:CStringToUnsignedChar
描述:将CString转换为unsigned char *
头文件:#include <atlconv.h>
**********************************************************************
unsigned char* CStringToUnsignedChar(CString csCommunitation,unsigned char *ucBuf)
{
USES_CONVERSION;
char *cMac = T2A(csCommunitation);
int nLongth = strlen(cMac)/2;
Convert(cMac,ucBuf,nLongth);
return ucBuf;
}
***********************************************************************
7、函数:DwordToCString
描述:将IP地址转换为CString
如将IP地址(如192.168.53.75)转换为一个CString类型的字符串(C0 A8 35 4B)
**********************************************************************
CString DwordToCString(CString csIp)
{
DWORD dwIp;
dwIp = htonl(csIp);
CString strMainServerIP;
DWORD dwMainServerIP = ntohl(dwIp);//网络序转到主机序
strMainServerIP.Format(_T("%02X%02X%02X%02X"),
(dwMainServerIP>>24)&0xff,
(dwMainServerIP>>16)&0xff,
(dwMainServerIP>>8)&0xff,
dwMainServerIP&0xff ) ;
return strMainServerIP;
}
***********************************************************************
8、函数:CStringTodwIP
描述:将CString的IP地址转换为DWORD类型的IP
如将一个CString类型的字符串(C0 A8 35 4B)转换为IP地址(如192.168.53.75)
**********************************************************************
DWORD CStringTodwIP( CString strIP )
{
DWORD dwRet = 0;
CString strIPTemp = _T("");
CString strLeft = _T("");
CString strRight= _T("");
int iPos = 0,iLen = 0;
strIPTemp = strIP.GetBuffer(0);
strRight = strIPTemp;
iPos=strRight.Find(_T("."),0);
iLen=strRight.GetLength();
while (iPos>0)
{
strLeft = strRight.Left(iPos);
dwRet=(dwRet<<8) + (unsigned)_ttoi(strLeft);
strRight = strRight.Right(iLen - iPos - 1);
iLen=strRight.GetLength();
iPos=strRight.Find(_T("."),0);
}
dwRet=(dwRet<<8) + (unsigned)_ttoi(strRight);
return dwRet;
}
(1)短字符与长字符的区别
所谓的短字符,就是8bit来表示的字符,典型的应用是ASCII码,而宽字符,顾名思义,就是用16bit表示的字符,典型的有UNICODE
Char实际上就是unsigned char,WCHAR为宽字符,而TCHAR根据是否支持unicode而不同。在程序使用sizeof(TCHAR),当默认设置时,这个值是1;当定义UNICODE宏时,这个值是2
***********************************************************************
9、函数:TransCStringToTCHAR
描述:将CString转换为TCHAR*
头文件:#include <windows.h>
**********************************************************************
TCHAR* CStringToCHAR(CString & str)
{
int nLen = str.GetLength();
TCHAR* szRs = new TCHAR[nLen];
lstrcpy(szRs,str.GetBuffer(nLen));
str.ReleaseBuffer();
return szRs;
}
***********************************************************************
10、函数:THCARToChar
描述:将TCHAR*转换为char *
***********************************************************************
char * THCARToChar (TCHAR* tchStr)
{
int nLen = 2*wcslen(tchStr);//CString,TCHAR 汉字算一个字符,因此不用普通计算 长度
char* chRtn = new char[nLen +1]
wcstombs(chRtn,tchStr,nLen +1);// 转换成功返回为非负值
return chRtn;
}
***********************************************************************
11、函数:GetAnsiString
描述:将 CString ( unicode )转换为 char*(ANSI)
***********************************************************************
char* GetAnsiString(const CString &s)
{
int nSize = 2*s.GetLength();
char *pAnsiString = new char[nSize+1];
wcstombs(pAnsiString, s, nSize+1);
return pAnsiString;
}
***********************************************************************
***********************************************************************
12、函数:TransCStringToTCHAR
描述:将CString转换为TCHAR*
头文件:#include <windows.h>
**********************************************************************
TCHAR* CStringToCHAR(CString & str)
{
int nLen = str.GetLength();
TCHAR* szRs = new TCHAR[nLen];
lstrcpy(szRs,str.GetBuffer(nLen));
str.ReleaseBuffer();
return szRs;
}
***********************************************************************
13、函数:THCARToChar
描述:将TCHAR*转换为char *
***********************************************************************
char * THCARToChar (TCHAR* tchStr)
{
int nLen = 2*wcslen(tchStr);//CString,TCHAR 汉字算一个字符,因此不用普通计算 长度
char* chRtn = new char[nLen +1]
wcstombs(chRtn,tchStr,nLen +1);// 转换成功返回为非负值
return chRtn;
}
***********************************************************************
14、函数:GetAnsiString
描述:将 CString ( unicode )转换为 char*(ANSI)
***********************************************************************
char* GetAnsiString(const CString &s)
{
int nSize = 2*s.GetLength();
char *pAnsiString = new char[nSize+1];
wcstombs(pAnsiString, s, nSize+1);
return pAnsiString;
}
***********************************************************************
15、
描述:将const char * 转换为CString
**********************************************************************
CString TransLPCSTRToCString(const char *)
{
int len = strlen(pErrMsg);
TCHAR* c1 = (TCHAR*)malloc(sizeof(TCHAR)*len);
MultiByteToWideChar( CP_ACP , 0 , pErrMsg , len+1 , c1 , len+1);
csText.Format(L"%s",pErrMsg);
retrun csText;
}
******************************************************
16、函数:CStringToLPCSTR
描述:将CString转换为const char *
*******************************************************
const char* CStrToLPCTSTR(CString strSrc)
{
DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, strSrc.GetBuffer(0), -1, NULL, 0, NULL, FALSE);
char *psText;
psText = new char[dwNum];
if (!psText)
delete []psText;
WideCharToMultiByte(CP_OEMCP, NULL, strSrc.GetBuffer(0), -1, psText, dwNum, NULL, FALSE);
return (const char*)psText;
}
******************************************************
17、函数:WCharToUTF8
描述:将const TCHAR*(LPCTSTR)转换为CHAR *(LPTSTR)
*******************************************************
std::string WCharToUTF8(LPCTSTR wszBuffer)
{
UINT nLen = WideCharToMultiByte(CP_UTF8,0,wszBuffer,-1,NULL,0,NULL,NULL);
CHAR* szAscii = new CHAR[nLen+1];
WideCharToMultiByte(CP_UTF8, NULL, wszBuffer,-1, szAscii, nLen, NULL, NULL);
std::string ret = szAscii;
delete[] szAscii;
return ret;
}
******************************************************
18、函数:UTF8ToChar
描述:将CHAR *(LPTSTR)转换为const TCHAR*(LPCTSTR)
*******************************************************
std::string UTF8ToChar(LPCSTR szUTF8)
{
UINT nLen = MultiByteToWideChar(CP_UTF8, NULL, szUTF8, -1, NULL, NULL);
WCHAR *wszBuffer = new WCHAR[nLen+1];
nLen = MultiByteToWideChar(CP_UTF8,NULL,szUTF8,-1,wszBuffer,nLen);
wszBuffer[nLen] = '\0';
nLen = WideCharToMultiByte(CP_ACP,0,wszBuffer,-1,NULL,0,NULL,NULL);
CHAR* szAscii = new CHAR[nLen+1];
WideCharToMultiByte(CP_ACP,NULL,wszBuffer,-1,szAscii,nLen,NULL,NULL);
szAscii[nLen]='\0';
std::string ret = szAscii;
//清理内存
delete[] wszBuffer;
delete[] szAscii;
return ret;
}
***********************************************************************
19、将CString转LPWSTR(即wchar_*)
USES_CONVERSION;
LPWSTR lpwStr = A2W(m_csUserName);
**********************************************************************
***********************************************************************
20、将CString转LPWSTR(即wchar_*)
USES_CONVERSION;
LPWSTR lpwStr = A2W(m_csUserName);
**********************************************************************
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有