现在的位置: 首页 > 综合 > 正文

字符串之间的转换

2018年02月12日 ⁄ 综合 ⁄ 共 4571字 ⁄ 字号 评论关闭


MFC中WString与string互相转换

 

  1. //Converting a WChar string to a Ansi string  
  2. std::string WChar2Ansi(const wchar_t* pwszSrc)  
  3. {  
  4.     int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);  
  5.     if (nLen<= 0)   
  6.         return std::string("");  
  7.     char* pszDst = new char[nLen];  
  8.     if (NULL == pszDst)   
  9.         return std::string("");  
  10.     WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);  
  11.     pszDst[nLen -1] = 0;  
  12.   
  13.     std::string strTemp(pszDst);  
  14.     delete [] pszDst;  
  15.     return strTemp;  
  16. }  
  17.   
  18. std::string ws2s(const std::wstring& inputws)  
  19. {  
  20.     return WChar2Ansi(inputws.c_str());   
  21. }  
  22.   
  23. //Converting a Ansi string to WChar string  
  24. std::wstring Ansi2WChar(const char* pszSrc, int nLen)  
  25. {  
  26.     int nSize = MultiByteToWideChar(CP_ACP, 0, (char*)pszSrc, nLen, 0, 0);  
  27.     if(nSize <= 0)   
  28.         return std::wstring(L"");  
  29.     wchar_t *pwszDst = new wchar_t[nSize+1];  
  30.   
  31.     if( NULL == pwszDst)  
  32.         return std::wstring(L"");  
  33.   
  34.     MultiByteToWideChar(CP_ACP, 0,(char*)pszSrc, nLen, pwszDst, nSize);  
  35.     pwszDst[nSize] = 0;  
  36.     if( pwszDst[0] == 0xFEFF) // skip Oxfeff  
  37.     {  
  38.         for(int i = 0; i < nSize; i ++)   
  39.         {  
  40.             pwszDst[i] = pwszDst[i+1];   
  41.         }  
  42.     }  
  43.   
  44.     std::wstring wcharString(pwszDst);  
  45.     delete pwszDst;  
  46.     return wcharString;  
  47. }  
  48.   
  49. std::wstring s2ws(const std::string& s)  
  50. {  
  51.     return Ansi2WChar(s.c_str(), (int)s.size());  
  52. }  

事实上stdlib.h中提供了更方便的函数;

  1. #include <cstdlib>  
  2. std::wstring s2ws(const char* source, int nCount)  
  3. {  
  4.  wchar_t* wdata = new wchar_t[nCount + 1];  
  5.  int n = mbstowcs(wdata, source, nCount);  
  6.  wdata[nCount] = L'/0';  
  7.  wstring ws(wdata);  
  8.  delete []wdata;  
  9.  return ws;  
  10. }  
  11.   
  12. std::string ws2s(const wchar_t *source, int nCount)  
  13. {  
  14.  char *sdata = new char[nCount + 1];  
  15.  wcstombs(sdata, source, nCount);  
  16.  sdata[nCount] = '/0';  
  17.  string s(sdata);  
  18.  delete []sdata;  
  19.  return s;  
  20. }  

数字与字符的转换:

可以使用atoi/atof将字符专程数字。而将数字转字符除了可以使用sprintf(),c++还可以使用stringstream,比如一个例子是:

  1. #include <string>  
  2. #include <sstream>  
  3. #include <iostream>  
  4.   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     stringstream ss;  
  10.     ss.setf(ios::fixed);  
  11.     double   d = 234234241.234567890123;  
  12.     ss << d;  
  13.     string   str=ss.str();  
  14.     cout << str;  
  15.     cout.setf(ios::fixed);  
  16.     cout << d;  
  17.     return 0;  
  18. }  

 最后,如果要清空ss,只需要ss.str("")即可。

 

需要注意的是stringstream默认的精度只有7位,因此0.0000001就会被转换成0。为此,需要设置精度。

#include <limits>

int prec=numeric_limits<long double>::digits10; // 18
stringstream ss;
ss.precision(prec);//覆盖默认精度

P.S.

 

格式化标志:  使用setf(fmtflags)函数打开标志,使用unsetf(fmtflags)函数关闭标志
开/关标志                作用
ios::skipws            跳过空格(输入流的默认情况)
ios::showbase      打印整型值时指出数字的计数(比如,为dec、oct或hex),showbase标志处于开状态时,输入流也能识别前缀基数
ios::showpoint      显示浮点值的小数点并截断数字末尾的零
ios::uppercase      显示十六进制值时使用大写A~F,显示科学计数中的E
ios::showpos        显示正数前的加号(+)
ios::unitbuf            “单元缓冲区”。每次插入后刷新流

转自:http://blog.csdn.net/joyyoj/article/details/4768853

抱歉!评论已关闭.