字符串转换:
1.wstring转string
- string WCharToAnsi(LPCWSTR pwszSrc)
- {
- int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
- if (nLen<= 0) return std::string("");
- char* pszDst = new char[nLen];
- if (NULL == pszDst) return std::string("");
- WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
- pszDst[nLen -1] = 0;
- std::string strTemp(pszDst);
- delete [] pszDst;
- return strTemp;
- }
- string ws2s(wstring& inputws){ return WCharToAnsi(inputws.c_str()); }
2.string转wstring
- std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)
- {
- int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
- if(nSize <= 0) return L"";
- WCHAR *pwszDst = new WCHAR[nSize+1];
- if( NULL == pwszDst) return L"";
- MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
- pwszDst[nSize] = 0;
- if( pwszDst[0] == 0xFEFF) // skip Oxfeff
- for(int i = 0; i < nSize; i ++)
- pwszDst[i] = pwszDst[i+1];
- wstring wcharString(pwszDst);
- delete pwszDst;
- return wcharString;
- }
3.wstring转UTF8
- string WCharToUTF8(wstring strUnicode)
- {
- //MessageBox(strUnicode);
- int ilen = WideCharToMultiByte(CP_UTF8, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);
- char *szUtf8Temp=new char[ilen + 4];
- memset(szUtf8Temp, 0, ilen + 4);
- szUtf8Temp[0] = 0xEF;
- szUtf8Temp[1] = 0xBB;
- szUtf8Temp[2] = 0xBF;
- WideCharToMultiByte (CP_UTF8, 0, strUnicode.c_str(), -1, &szUtf8Temp[3], ilen, NULL,NULL);
- ilen += 3;
- szUtf8Temp[ilen - 1] = 0;
- std::string strTemp(szUtf8Temp);
- delete [] szUtf8Temp;
- return strTemp;
- }
4.UTF8转wstring
- wstring UTF8ToWChar(LPCSTR pszSrc, int nLen)
- {
- int nSize = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)pszSrc, nLen, 0, 0);
- if(nSize <= 0) return L"";
- WCHAR *pwszDst = new WCHAR[nSize+1];
- if( NULL == pwszDst) return L"";
- MultiByteToWideChar(CP_UTF8, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
- pwszDst[nSize] = 0;
- if( pwszDst[0] == 0xFEFF) // skip Oxfeff
- for(int i = 0; i < nSize; i ++)
- pwszDst[i] = pwszDst[i+1];
- wstring wcharString(pwszDst);
- delete pwszDst;
- return wcharString;
- }
文件操作:
1.格式化文件大小
- #include <sstream>
- #include <iomanip>
- #ifdef UNICODE
- #define tstring std::wstring
- #define tostringstream std::wostringstream
- #else
- #define tstring std::string
- #define tostringstream std::ostringstream
- #endif
- std::wstring FormatFileSize( size_t nSize )
- {
- tostringstream oss;
- oss<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
- if (nSize > 1024*1024*1024)
- oss << (double)nSize/1024/1024/1024 << tstring(_T("GB"));
- else if (nSize > 1024*1024)
- oss << (double)nSize/1024/1024 << tstring(_T("MB"));
- else if (nSize > 1024)
- oss << (double)nSize/1024 << tstring(_T("KB"));
- else if (nSize > 0)
- oss << nSize << tstring(_T("B"));
- else
- oss << tstring(_T("未知"));
- return oss.str();
- }
2.获取文件名和扩展名
- // 获取文件名
- wstring GetFileName(wstring strFilename)
- {
- int npos = strFilename.rfind('\\');
- return strFilename.substr(npos == string::npos ? strFilename.length() : npos+1);
- }
- // 获取扩展名
- wstring GetFileExt(wstring strFilename)
- {
- int npos = strFilename.rfind('.');
- return strFilename.substr(npos == string::npos ? strFilename.length() : npos+1);
- }