/************************************************************************/ /* 该类主要实现常用数据类型之间的转换, */ /************************************************************************/ #ifndef _TYPE_CONVERT_ #define _TYPE_CONVERT_ #include <string> #include <Windows.h> using namespace std; //多字节转宽字节 static bool MultiCharToWideChar(wstring& DesStr, string SourStr) { long SourStrLength = MultiByteToWideChar(CP_ACP, 0, SourStr.c_str(), -1, NULL, 0); WCHAR * wcStrTemp = new WCHAR[SourStrLength]; if(!wcStrTemp) { delete []wcStrTemp; return false; } MultiByteToWideChar(CP_ACP, 0, SourStr.c_str(), -1, wcStrTemp, SourStrLength); DesStr = (wstring)wcStrTemp;//wchar*转换为wstring //DesStr.assign(wcStrTemp); //同上 delete [] wcStrTemp; return true; } //宽字节转多字节 static bool WideCharToMulitChar(string& DesStr, wstring SourStr) { long SourstrLength = WideCharToMultiByte(CP_ACP, 0, SourStr.c_str(), -1, NULL, 0, NULL, NULL); char * cStrTemp = new char[SourstrLength]; if(!cStrTemp) { delete []cStrTemp; return false; } WideCharToMultiByte(CP_ACP, 0, SourStr.c_str(), -1, cStrTemp, SourstrLength, NULL, NULL); DesStr = (string)cStrTemp;//char*转换为string delete [] cStrTemp; return true; } //将string转换为LPCTSTR类型 static void StrToLPCTSTR(LPCTSTR& lpctstrDest, string SourStr) { lpctstrDest = (LPCTSTR)(LPTSTR)SourStr.c_str(); } static void WtrToLPCTSTR(LPCTSTR& lpctstrDest, wstring SourStr) { lpctstrDest = (LPCTSTR)(LPTSTR)SourStr.c_str(); } //整数装换为string static void IntegerToStr(char* strDest, long lInterger) { // itoa(lInterger, strDest, 10); //将strDest转换为字符串,十进制 // itoa(lInterger, strDest, 2); //按二进制方式转换 ltoa(lInterger, strDest, 10); //长整型long } static void StringToConstCharPointer() { string strTemp; //strTemp.c_str(); wstring wtrTemp; //wtrTemp.c_str(); } //const char* To char* static void ConstcharPointerTocharPointer(char* strDestination ,const char* strSource) { strDestination = new char[strlen(strSource )+1]; strcpy(strDestination, strSource ); } static void ConstWcharPointerToWcharPointer(WCHAR *strDestination, const WCHAR *strSource) { strDestination = new WCHAR[wcslen(strSource )+1]; wcscpy(strDestination, strSource); } //#include <cstringt.h> //MFC-only string objects #include <atlstr.h> //Non-MFC string objects static void IntToCString(CString &strDest, int Integer) { strDest.Format(TEXT("%d"), Integer); } static void StringToCString(CString &strDest, string strSour) { strDest.Format(TEXT("%s"), strSour.c_str()); } static void CharPointerToCString(CString &strDest, char *cSour) { strDest.Format(TEXT("%s"), cSour); } static void CStringToDouble(double iDest, CString &strSour) { iDest = _wtof(strSour.GetString()); //atoi _atoi64或atol } static void CStringToString(wstring strDest, CString &strSour) { strDest = strSour.GetBuffer(0); strSour.ReleaseBuffer(); } static void DoubleToString(double dSour, string strDest) { char TempBuf[32]; sprintf(TempBuf,"%.6f",dSour);//精度控制转换为6位 strDest = TempBuf; } static void DoubleToCString(double dDest, CString strSour) { strSour.Format(TEXT("%.4f"),dDest); } /* atof, _atof_l, _wtof, _wtof_l Convert string to float atoi, _atoi_l, _wtoi, _wtoi_l Convert string to int _atoi64, _atoi64_l, _wtoi64, _wtoi64_l Convert string to __int64 atol, _atol_l, _wtol, _wtol_l Convert string to long _itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow, _itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_s Convert int or __int64 to string _ltoa, _ltow, _ltoa_s, _ltow_s Convert long to string strtod, _strtod_l, wcstod, _wcstod_l Convert string to double strtol, wcstol, _strtol_l, _wcstol_l Convert string to long integer strtoul, _strtoul_l, wcstoul, _wcstoul_l Convert string to unsigned long integer _ultoa, _ultow, _ultoa_s, _ultow_s Convert unsigned long to string atof, _atof_l, _wtof, _wtof_l Convert wide-character string to a double atoi, _atoi_l, _wtoi, _wtoi_l Convert wide-character string to int _atoi64, _atoi64_l, _wtoi64, _wtoi64_l Convert wide-character string to __int64 atol, _atol_l, _wtol, _wtol_l */ /*关于const char*, char const* and char *const 等的区别,助记方法:把一个声明从右向左读,比如: char * const cp;//cp is a const pointer to char const char * cp;10 //cp is a pointer to const char; 规则:从右到左读: // * 读成 pointer to // ** 读成 (a) pointer to (a) pointer to // * const 读成 const pointer to char ** cpp; //cpp is a pointer to ( a pointer to char ) const char ** cpp; // cpp is a pointer to ( a pointer to const char ) char * const * cpp; //cpp is a pointer to const pointer to char const char * const * cpp; //cpp is pointer to const pointer to const char char ** const cpp; //cpp is const pointer to pointer to char char * const * const cpp;//cpp is const pointer to const pointer to char const char * const * const cpp; //cpp is const pointer to const pointer to const char //×××××××××××××××××××××××××××××××××××××××××××× //关键:只要记住:(1)把声明从右到左读(2)* 读成 pointer to */ /* LPARAM lParam; WORD loValue = LOWORD(lParam);///取低16位 WORD hiValue = HIWORD(lParam);///取高16位 对于16位的数据(WORD)我们可以用同样的方法分解成高低两个8位数据(BYTE),例如: WORD wValue; BYTE loValue = LOBYTE(wValue);///取低8位 BYTE hiValue = HIBYTE(wValue);///取高8位 */ #endif