#include "pch.h" #include "BaseUtility.h" #include #include #include #include #include #include #include "Base64.h" typedef basic_string::size_type S_T; bool IsLeap(int year) { if((year%4==0&&year%100!=0)||year%400==0) return true; else return false; } bool IsWeekEnd(int year, int month, int day) { struct tm stTime = {0}; struct tm *ptr = NULL; time_t time; stTime.tm_year = year - 1900; stTime.tm_mon = month - 1; stTime.tm_mday = day; time = mktime(&stTime); ptr = localtime(&time); return (ptr->tm_wday == 0 || ptr->tm_wday == 6); } bool Nextday(int& year, int& month, int& day) { short ld[12]={31,28,31,30,31,30,31,31,30,31,30,31}; if (!(!(year%100)&&(year%400)) && !(year%4)) ld[1]++; day++; if (day<=0 || day>(unsigned long)ld[month-1]) { day = 1; month++; if(month>12) { month = 1; year++; } } return true; } void Full2Half(wchar_t* str) { if(str != NULL) { int len = wcslen(str); int i = 0; wchar_t space[] = L" "; for(; i= 65281 && str[i] <= 65374) { str[i] -= (65281 - 33); } } } } bool split(const tstring& src, tstring delimit,vector &v, tstring null_subst) { if(src.empty() || delimit.empty()) return false; S_T deli_len = delimit.size(); long index = -1,last_search_position = 0; while( (index=src.find(delimit,last_search_position))!=-1 ) { if(index==last_search_position) v.push_back(null_subst); else v.push_back( src.substr(last_search_position, index-last_search_position) ); last_search_position = index + deli_len; } tstring last_one = src.substr(last_search_position); v.push_back( last_one.empty()? null_subst:last_one ); return true; } bool split(const string& src, string delimit, vector &v, string null_subst) { if (src.empty() || delimit.empty()) return false; S_T deli_len = delimit.size(); long index = -1, last_search_position = 0; while ((index = src.find(delimit, last_search_position)) != -1) { if (index == last_search_position) v.push_back(null_subst); else v.push_back(src.substr(last_search_position, index - last_search_position)); last_search_position = index + deli_len; } string last_one = src.substr(last_search_position); v.push_back(last_one.empty() ? null_subst : last_one); return true; } vector splitEx(const tstring& src, tstring separate_character) { vector strs; int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符 int lastPosition = 0, index = -1; while (-1 != (index = src.find(separate_character, lastPosition))) { strs.push_back(src.substr(lastPosition, index - lastPosition)); lastPosition = index + separate_characterLen; } tstring lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容 if (!lastString.empty()) strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队 return strs; } vector splitEx(const string& src, string separate_character) { vector strs; int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符 int lastPosition = 0, index = -1; while (-1 != (index = src.find(separate_character, lastPosition))) { strs.push_back(src.substr(lastPosition, index - lastPosition)); lastPosition = index + separate_characterLen; } string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容 if (!lastString.empty()) strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队 return strs; } int GB2312_2_UTF8(char* buf, int buf_len, const char* src, int src_len) { int i = 0, j = 0; if (0 == src_len) { src_len = strlen(src); } for (i = 0; i < src_len;) { if (j >= buf_len - 1) break; if (src[i] >= 0) { buf[j++] = src[i++]; } else { unsigned short w_c = 0; char tmp[4] = ""; Gb2312_2_Unicode(&w_c, src + i); Unicode_2_UTF8(tmp, &w_c); buf[j+0] = tmp[0]; buf[j+1] = tmp[1]; buf[j+2] = tmp[2]; i += 2; j += 3; } } buf[j] = '\0'; return j; } void Gb2312_2_Unicode(unsigned short* dst, const char* src) { MultiByteToWideChar(936, MB_PRECOMPOSED, src, 2, (LPWSTR)dst, 1); } void Unicode_2_UTF8(char* dst, unsigned short* src) { char *pchar = (char *)src; dst[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4)); dst[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6); dst[2] = (0x80 | ( pchar[0] & 0x3F)); } string UTF8ToTstring(const char *pData, size_t size) { size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, NULL, 0); WCHAR *pChar = new WCHAR[n+1]; n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, pChar, n); pChar[n]=0; n = WideCharToMultiByte(936, 0, pChar, -1, 0, 0, 0, 0); char *p = new char[n+1]; n = WideCharToMultiByte(936, 0, pChar, -1, p, (int)n, 0, 0); string result(p); delete []pChar; delete []p; return result; } string ConvertUTF8toGB2312(const char* pData, size_t size) { size_t n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, NULL, 0); WCHAR* pChar = new WCHAR[n + 1]; n = MultiByteToWideChar(CP_UTF8, 0, pData, (int)size, pChar, n); pChar[n] = 0; n = WideCharToMultiByte(936, 0, pChar, -1, 0, 0, 0, 0); char* p = new char[n + 1]; n = WideCharToMultiByte(936, 0, pChar, -1, p, (int)n, 0, 0); string result(p); delete[]pChar; delete[]p; return result; } string ConvertGB2312toUTF8(const char *pData) { int len = MultiByteToWideChar(936, 0, pData, -1, NULL, 0); wchar_t* wstr = new wchar_t[len+1]; memset(wstr, 0, len+1); MultiByteToWideChar(936, 0, pData, -1, wstr, len); len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); char* str = new char[len+1]; memset(str, 0, len+1); WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL); if(wstr) delete[] wstr; string strUtf8(str); if(str) delete[] str; return strUtf8; } wstring GB2312ToUnicode(const char *pData) { wstring str; int len = MultiByteToWideChar(936, 0, pData, -1, NULL, 0); // 先取得转换后的UNICODE字符串所需的长度 if (len <= 0) return str; wchar_t* pWstr = (wchar_t*)calloc(len + 1, sizeof(wchar_t)); // 分配缓冲区 MultiByteToWideChar(936, 0, pData, -1, pWstr, len); // 开始转换 str = pWstr; if (pWstr) free(pWstr); return str; } string UnicodeToGB2312(TCHAR *pData) { string str; int len = WideCharToMultiByte(936, 0, pData, -1, NULL, 0, NULL, NULL); // 先取得转换后的ANSI字符串所需的长度 if (len <= 0) return str; char* pStr = (char*)calloc(len + 1, sizeof(char)); // 分配缓冲区 WideCharToMultiByte(936, 0, pData, -1, pStr, len, NULL, NULL); // 开始转换 str = pStr; if (pStr) free(pStr); return str; } void UTF8toANSI(string &strUTF8) { //获取转换为多字节后需要的缓冲区大小,创建多字节缓冲区 UINT nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8.c_str(), -1, NULL, NULL); WCHAR *wszBuffer = new WCHAR[nLen + 1]; nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8.c_str(), -1, wszBuffer, nLen); wszBuffer[nLen] = 0; nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, NULL, NULL, NULL, NULL); char *szBuffer = new char[nLen + 1]; nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, szBuffer, nLen, NULL, NULL); szBuffer[nLen] = 0; strUTF8 = szBuffer; //清理内存 delete[]szBuffer; delete[]wszBuffer; } string UnicodeToGB2312(const tstring& strSrc) { return UnicodeToGB2312((LPTSTR)strSrc.c_str()); } string UnicodeToGB2312(const CString& strSrc) { tstring wstr = strSrc; return UnicodeToGB2312((LPTSTR)wstr.c_str()); } wstring UTF8ToUnicode(const char* szU8) { wstring str; if (szU8 == NULL) { return str; } //预转换,得到所需空间的大小; int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0); if (wcsLen <= 0) return str; //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间 wchar_t* pWStr = (wchar_t*)calloc(wcsLen + 1, sizeof(wchar_t)); //转换 ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), pWStr, wcsLen); pWStr[wcsLen] = L'\0'; str = pWStr; if (pWStr) free(pWStr); return str; } string UnicodeToUtf8(const wchar_t* unicode) { string str; int len; len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL); if (len <= 0) return str; char *szUtf8 = (char*)calloc(len + 1, sizeof(char)); WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL); szUtf8[len] = '\0'; str = szUtf8; if (szUtf8) free(szUtf8); return str; } tstring GB2312ToTstring(string str) { tstring tstr=_T(""); #ifdef _UNICODE tstr=GB2312ToUnicode(str.c_str()); #else tstr=str; #endif return tstr; } string TstringToGB2312(tstring tstr) { string str = ""; #ifdef _UNICODE str = UnicodeToGB2312(tstr); #else str = pData; #endif return str; } string TstringToUTF8(tstring tstr) { string str = ""; #ifdef _UNICODE str = UnicodeToUtf8(tstr.c_str()); #else char* p=ConvertGB2312toUTF8(tstr.c_str()); str=*p; delete[]p; #endif return str; } tstring UTF8ToTstring(string str) { tstring tstr = _T(""); #ifdef _UNICODE tstr = UTF8ToUnicode(str.c_str()); #else tstr = ConvertUTF8toGB2312(str.c_str(), str.length()); #endif return tstr; } bool TransToPriceFormat(const tstring& strSrc, tstring& strDes, double& dDes) { vector vctPrice; if (!split(strSrc, _T("|"), vctPrice)) return false; strDes.clear(); for (int i = 0; i < vctPrice.size(); i++) { if (vctPrice[i].length() == 0) continue; dDes += _tstof(vctPrice[i].c_str()); strDes.append(vctPrice[i]); if (i != vctPrice.size()-1) { strDes.append(_T("+")); } } return true; } int CalcWord(const char *ps, const size_t length) { ASSERT(0 < length); ASSERT(NULL != ps); int nRealLen(0); int nFlag(0); while (nFlag <= length && 0 != ps[nRealLen]) { if (IsChinese(ps[nRealLen])) { nRealLen++; if (0 != ps[nRealLen]) nRealLen++; else break; } else nRealLen++; nFlag++; } return nRealLen; } void Int64DateToStr(const __int64& llDate, string& out) { int nYear = llDate / 10000000000L; int nMode = llDate % 10000000000L; int nMon = nMode / 100000000; nMode %= 100000000; int nDay = nMode / 1000000; nMode %= 1000000; int nHour = nMode / 10000; nMode %= 10000; int nMin = nMode / 100; int nSec = nMode % 100; char buf[20]; memset(buf, 0, 20); sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d", nYear,nMon,nDay,nHour,nMin,nSec); out = buf; } void Int64DateToYMDStr(const __int64& llDate, string& out) { int nYear = llDate / 10000000000L; int nMode = llDate % 10000000000L; int nMon = nMode / 100000000; nMode %= 100000000; int nDay = nMode / 1000000; nMode %= 1000000; char buf[20]; memset(buf, 0, 20); sprintf(buf, "%04d-%02d-%02d", nYear,nMon,nDay); out = buf; } void Int64DateToHMStr(const __int64& llDate, string& out) { int nYear = llDate / 10000000000L; int nMode = llDate % 10000000000L; int nMon = nMode / 100000000; nMode %= 100000000; int nDay = nMode / 1000000; nMode %= 1000000; int nHour = nMode / 10000; nMode %= 10000; int nMin = nMode / 100; char buf[20]; memset(buf, 0, 20); sprintf(buf, "%02d:%02d", nHour,nMin); out = buf; } unsigned char ToHex(unsigned char x) { return x > 9 ? x + 55 : x + 48; } unsigned char FromHex(unsigned char x) { unsigned char y; if (x >= 'A' && x <= 'Z') y = x - 'A' + 10; else if (x >= 'a' && x <= 'z') y = x - 'a' + 10; else if (x >= '0' && x <= '9') y = x - '0'; return y; } string UrlEncode(const string& str) { string strTemp = ""; size_t length = str.length(); for (size_t i = 0; i < length; i++) { if (isalnum((unsigned char)str[i]) || (str[i] == '-') || (str[i] == '_') || (str[i] == '.') || (str[i] == '~')) strTemp += str[i]; else if (str[i] == ' ') strTemp += "+"; else { strTemp += '%'; strTemp += ToHex((unsigned char)str[i] >> 4); strTemp += ToHex((unsigned char)str[i] % 16); } } return strTemp; } string UrlDecode(const string& str) { string strTemp = ""; size_t length = str.length(); for (size_t i = 0; i < length; i++) { if (str[i] == '+') strTemp += ' '; else if (str[i] == '%') { unsigned char high = FromHex((unsigned char)str[++i]); unsigned char low = FromHex((unsigned char)str[++i]); strTemp += high*16 + low; } else strTemp += str[i]; } return strTemp; } void DeleteDirectory(const CString &strPath) { if (strPath.IsEmpty()) return; CFileFind ff; BOOL bFound = ff.FindFile(strPath + _T("\\*"), 0); while(bFound) { bFound = ff.FindNextFile(); if ((ff.GetFileName() == _T(".")) || (ff.GetFileName() == _T(".."))) continue; SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL); if (ff.IsDirectory()) { DeleteDirectory(ff.GetFilePath()); RemoveDirectory(ff.GetFilePath()); } else { DeleteFile(ff.GetFilePath()); } } ff.Close(); //RemoveDirectory(strPath); } void toUpper(char *szDestination, const char *szSource) { if (szDestination == NULL || szSource == NULL || strlen(szSource) == 0) return; while (*szSource != 0) { if (*szSource >= 'a' && *szSource <= 'z') *szDestination++ = 'A' + (*szSource++ - 'a'); else *szDestination++ = *szSource++; } *szDestination = 0; } void toUpper(TCHAR *szDestination, const TCHAR *szSource) { if (szDestination == NULL || szSource == NULL || _tcslen(szSource) == 0) return; while (*szSource != 0) { if (*szSource >= 'a' && *szSource <= 'z') *szDestination++ = 'A' + (*szSource++ - 'a'); else *szDestination++ = *szSource++; } *szDestination = 0; } void FormatPrice(char *szDes, const double dSource) { sprintf(szDes, "%.9lf", dSource); int i(0); int nLen(strlen(szDes)); for (; i < nLen; i++) { if (szDes[i] == '.') break; } for (int j = nLen - 1; j > i + 2; j--) { if (szDes[j] == '0') szDes[j] = 0; else break; } } bool CombinationVolRemark(string &strDes, const char *szVol, const char *szRemark) { vector vctVol; vector vctRemark; bool bRet(true); if (!split(szVol, "|", vctVol)) bRet = false; if (!split(szRemark, "|", vctRemark)) bRet = false; if (vctVol.size() != vctRemark.size()) bRet = false; if (!bRet) { if (!split(szVol, "+", vctVol)) bRet = false; else bRet = true; if (!split(szRemark, "+", vctRemark)) bRet = false; else bRet = true; if (vctVol.size() != vctRemark.size()) bRet = false; } if (!bRet) return bRet; for (int i = 0; i < vctVol.size(); i++) { if (i != 0) strDes.append("+"); if (vctVol[i].empty()) strDes.append("--"); else strDes.append(vctVol[i]); if (!vctRemark[i].empty()) { strDes.append("("); strDes.append(vctRemark[i]); strDes.append(")"); } } return true; } string replace(const char *szContext, char oldChar, const char *szNew) { int nLen = strlen(szContext) + 1; if (szNew != NULL) nLen += strlen(szNew); char *pTemp = new char[nLen]; autoptr_arr arrguard(pTemp); memset(pTemp, 0, nLen); char *pResult = pTemp; while(*szContext != 0) { if (*szContext == oldChar) { if (szNew != NULL) { while (*szNew!= 0) { *pTemp++ = *szNew++; } } } else { *pTemp++ = *szContext; } szContext++; } return string(pResult); } bool CombinationQuoteData(string &strDes, const char *szVol, const char *szRemark/* = NULL*/ , const char *szDanger/* = NULL*/, const char *szOCO/* = NULL*/, const char *szPACK/* = NULL*/) { if (szVol == NULL) return false; vector vctData; vctData.push_back(szVol); if (szDanger != NULL) { vctData.push_back(szDanger); } if (szOCO != NULL) { vctData.push_back(szOCO); } if (szPACK != NULL) { vctData.push_back(szPACK); } if (szRemark != NULL && strlen(szRemark) > 0 && strcmp(szRemark, "--") != 0) { vctData.push_back(szRemark); } const char* arrSeparators[2] = {"|", "+"}; bool bMulti(false); int nIdx(0); for (int i = 0; i < 2; i++) { if (strstr(szVol, arrSeparators[i]) != NULL) { nIdx = i; bMulti = true; break; } } if (bMulti) { vector> vctSplit; for (int i = 0; i < vctData.size(); i++) { vector vct; if (!split(vctData[i], arrSeparators[nIdx], vct)) { return false; } vctSplit.push_back(vct); } int nCount(vctSplit[0].size()); for (int i = 1; i < vctSplit.size(); i++) { if (nCount != vctSplit[i].size()) return false; } for (int i = 0; i < nCount; i++) { if (i != 0) strDes.append("+"); if (strlen((vctSplit[0])[i].c_str()) == 0) strDes.append("--"); else { strDes.append((vctSplit[0])[i]); } string strTemp; for (int j = 1; j < vctSplit.size(); j++) { if (strlen((vctSplit[j])[i].c_str()) != 0) { strTemp.append((vctSplit[j])[i]); strTemp.append(","); } } if (!strTemp.empty()) { if (strTemp[strTemp.size() - 1] == ',') { strTemp = string(strTemp.c_str(), strTemp.size() - 1); } } if (!strTemp.empty()) { strDes.append("("); strDes.append(strTemp); strDes.append(")"); } } } else { if (strlen(szVol) == 0) { strDes.append("--"); } else { strDes.append(szVol); } string strTemp; for (int i = 1; i < vctData.size(); i++) { if (strlen(vctData[i]) != 0) { strTemp.append(vctData[i]); strTemp.append(","); } } if (!strTemp.empty()) { if (strTemp[strTemp.size() - 1] == ',') { strTemp = string(strTemp.c_str(), strTemp.size() - 1); } } if (!strTemp.empty()) { strDes.append("("); strDes.append(strTemp); strDes.append(")"); } } return true; } __int64 StringToInt64(const TCHAR *szSource) { __int64 ret(0); if (_tcslen(szSource) == 0) return ret; const TCHAR *pTemp = szSource + _tcslen(szSource) - 1; __int64 nRight(1); bool bNegative(false); if ('-' == szSource[0]) bNegative = true; do { if (*pTemp >= '0' && *pTemp <= '9') { ret += nRight * (0 + (*pTemp - '0')); nRight *= 10; } } while(pTemp-- != szSource); if (bNegative) ret = -ret; return ret; } void SafeCopyData(TCHAR dest[], int destSize, TCHAR* src) { int srcSize = _tcslen(src); if (srcSize == 0) { return; } if (srcSize > destSize) memcpy(dest, src, destSize); else _tcscpy(dest, src); } void SafeCopyData(TCHAR dest[], int destSize, tstring str) { int srcSize = str.length(); if (srcSize == 0) { return; } if (srcSize > destSize) memcpy(dest, str.c_str(), destSize); else _tcscpy(dest, str.c_str()); } tstring GetRequestByKey(const tstring& tstrKey, const tstring& tstrReuest) { string strKey = UnicodeToGB2312(tstrKey); string strReuest = UnicodeToGB2312(tstrReuest); try { smatch result; if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*?)&"))) { return GB2312ToUnicode(result[1].str().c_str()); } else if (regex_search(strReuest.cbegin(), strReuest.cend(), result, regex(strKey + "=(.*)"))) { return GB2312ToUnicode(result[1].str().c_str()); } else { return tstring(); } } catch (...) { return tstring(); } return tstring(); } void GetKeysByUrl(const tstring & strUrl, vector & vctKey) { vector vctSearch; split(strUrl, _T("?"), vctSearch); if (vctSearch.size() != 2 && vctSearch.size() != 1) { return; } tstring strTemp; if (vctSearch.size() == 1) { strTemp = vctSearch[0]; } else if (vctSearch.size() == 2) { strTemp = vctSearch[1]; } vctSearch.clear(); split(strTemp, _T("&"), vctSearch); if (vctSearch.size() <= 0) { return; } for (int i = 0; i < vctSearch.size(); i++) { strTemp = vctSearch[i]; vector vct; split(strTemp, _T("="), vct); if (vct.size() == 2) { vctKey.push_back(vct[0]); } } } __int64 GetCharNumber(TCHAR* szText, size_t n) { __int64 nDest; if (n == 0) { return -1; } for (nDest = 0; n--; szText++) { if (*szText < '0' || *szText > '9') { continue; //继续获取下一个字符 } /* 1、当前字符值 *line的ascii码,减去字符0的ascii码,得出个位数字 2、原计算的value值 乘以10,向上提升一位 3、二者相加得到新的十进制数值 */ nDest = nDest * 10 + (*szText - '0'); } if (nDest < 0) { return -1; } else { return nDest; } } tstring ANSIToUnicode(const string& str) { int len = 0; len = str.length(); int unicodeLen = ::MultiByteToWideChar(936, 0, str.c_str(), -1, NULL, 0); wchar_t * pUnicode; pUnicode = new wchar_t[unicodeLen + 1]; memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t)); ::MultiByteToWideChar(936, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen); tstring rt; rt = (wchar_t*)pUnicode; delete pUnicode; return rt; } CString AddNumberSeparate(CString strSrc) { auto IsNumberFunc = [=](CString str) { int nCount = 0; for (int i = 0; i < str.GetLength(); i++) { if (i == 0) { if (str.GetAt(i) == '-') { continue; } } if (!((str.GetAt(i) >= '0' && str.GetAt(i) <= '9') || str.GetAt(i) == '.')) { return FALSE; } if (str.GetAt(i) == '.') { nCount++; } } if (nCount > 1) { return FALSE; } return TRUE; }; CString str(strSrc); if (IsNumberFunc(str)) { int nPos = 0; nPos = str.Find(_T("."), nPos); vector vcteparatePos; if (nPos == -1) { for (int i = str.GetLength() - 3; i > 0; i--) { if ((str.GetLength() - i) % 3 == 0) { if (!(i == 1 && str.GetAt(0) == '-')) { vcteparatePos.push_back(i); } } } } else { for (int i = nPos - 3; i > 0; i--) { if ((nPos - i) % 3 == 0) { if (!(i == 1 && str.GetAt(0) == '-')) { vcteparatePos.push_back(i); } } } } for (int i = 0; i < vcteparatePos.size(); i++) { str.Insert(vcteparatePos[i], ','); } } return str; } bool RegexStdMatch(string src, string regular) { regex pattern(regular.c_str()); if (!regex_match(src, pattern)) { return false; } return true; } bool RegexStdMatch(tstring src, tstring regular) { char* pre = setlocale(LC_ALL, ""); setlocale(LC_ALL, "chs"); std::wregex pattern(regular.c_str()); if (!regex_match(src, pattern)) { return false; } return true; } CString FormatNumberString(CString strData) { TCHAR szTemp[32] = { 0 }; _stprintf(szTemp, _T("%0.4lf"), _tstof(strData)); CString str = szTemp; str.TrimRight(_T("0")); if (str.Find(_T(".")) == (str.GetLength() - 1)) str.TrimRight(_T(".")); return str; } tstring replace_all(tstring& str, const tstring& old_value, const tstring& new_value) { while (true) { tstring::size_type pos(0); if ((pos = str.find(old_value)) != tstring::npos) str.replace(pos, old_value.length(), new_value); else break; } return str; } tstring FormatInt32Value(int nValue) { TCHAR szTemp[64] = { 0 }; _stprintf(szTemp, _T("%d"), nValue); return tstring(szTemp); } string FormatInt32Value2(int nValue) { char szTemp[64] = { 0 }; sprintf(szTemp, "%d", nValue); return string(szTemp); } tstring FormatInt64Value(__int64 nValue) { TCHAR szTemp[64] = { 0 }; _stprintf(szTemp, _T("%lld"), nValue); return tstring(szTemp); } tstring FormatFloatValue(double fValue) { TCHAR szTemp[32] = { 0 }; _stprintf(szTemp, _T("%0.3lf"), fValue); CString str = szTemp; str.TrimRight(_T("0")); if (str.Find(_T(".")) == (str.GetLength() - 1)) str.TrimRight(_T(".")); return tstring(str); } string FormatFloatValue2(double fValue) { char szTemp[64] = { 0 }; sprintf(szTemp, "%0.3lf", fValue); return string(szTemp); } CString GetAppPath() { CString strRetun = _T(""); #ifdef _UNICODE TCHAR szBuff[MAX_PATH]; HMODULE module = GetModuleHandle(0); GetModuleFileName(module, szBuff, sizeof(szBuff)); strRetun.Format(_T("%s"), szBuff); #else HMODULE module = GetModuleHandle(0); CHAR szBuff[MAX_PATH]; GetModuleFileName(module, szBuff, sizeof(szBuff)); strRetun.Format(_T("%s"), szBuff); #endif int pos = strRetun.ReverseFind(_T('\\')); if (pos != -1) { strRetun = strRetun.Left(pos); } return strRetun; } string GetImageEncodeString(string strPath) { ifstream is(strPath.c_str(), ifstream::in | ios::binary); is.seekg(0, is.end); int length = is.tellg(); is.seekg(0, is.beg); char *buffer = new char[length]; is.read(buffer, length); //主要注意这一句是利用base64.h文件中的编码函数将buffer编码为base64码 CBase64Coder base64; std::string strImg = base64.encode(buffer, length); string strTemp = "data:image/jpeg;base64,"; strTemp.append(strImg); //打印img看一下 //string strEncode = UrlEncode(strTemp); //删除buffer delete[]buffer; //关闭指针 is.close(); return strTemp; }