C++和VC++中各種類型轉換

來源:互聯網
上載者:User
在做VC開發時經常遇到c++與VC++類型不符的錯誤,需要上網查資料,因為本人的記憶很差,經常出現同樣的錯誤,每次都要上網去查,現將各類類型轉換總結如下,並把其陸續補齊,我會在以後更新,供自己後者大家查閱,以後不必再在網上海尋了,

1 string 2 CString   

  CString.format("%s",string.c_str()); 

2 CString 2 string

string str(CString.GetBuffer(str.GetLength()));

3 string 2 char *

char *p=string.c_str();

4 char * 2 string

string str(char*);

5 CString 2 char *

strcpy(char,CString,sizeof(char));

6 char * 2 CString

CString.format("%s",char*);

CString的format方法是非常好用的。string的c_str()也是非常常用的,但要注意和char *轉換時,要把char定義成為const char*,這樣是最安全的。

************************************************************************************************

1. char* to string
string s(char *); 
註:在不是初始化的地方最好用assign().
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2. string to const char*
string a="strte";
const char* r=a.c_str();
注意是const的。還要轉到char*:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.2. const char* to char*
const char* r="123";
char   *p1   =   new   char[strlen(r)+1];
strcpy(p1,r);
附:http://hi.baidu.com/cfans/blog/item/06970ef4b671f366dcc4745d.html
 這個頁面是具體講述區別的。
·············································································································
3. cstring to string
vs2005 Unicode下:
  CStringW   str(L"test");  
  CStringA   stra(str.GetBuffer(0));  
  str.ReleaseBuffer();      
  std::string   strs   (stra.GetBuffer(0));  
  stra.ReleaseBuffer();

非Unicode下:
CString cs("test");
std::string str=cs.getBuffer(0);
cs.ReleaseBuffer();

註:GetBuffer()後一定要ReleaseBuffer(),否則就沒有釋放緩衝區所佔的空間.
++++++++++++++++++++++++++++++++++++++++++++++++++++
4. double ,int to string
#include <sstream>
using namespace std;

stringstream ss;
string result;
long n=11111;
stream << n; //從long型資料輸入
stream >>result; //轉換為 string

===================================================

5.char*  to int, double ,long

char *s; double x; int i; long l;

s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );

s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s/tfloat: %e/n", s, x );

s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s/t/tinteger: %d/n", s, i );

s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s/t/tlong: %ld/n", s, l );
------------------------------------------------------------------------------------------------
6. string to int ,long ,double            
              int s;
 string str="123";
 stringstream ss;
 ss<<str;//從str輸入
 ss>>s;//輸出到int
 ss.clear();

——————————————————————————————————————————
7. date to string
#include <time>
using namespace std;

char dateStr [9];
char timeStr [9];
 _strdate( dateStr);
printf( "The current date is %s /n", dateStr);
_strtime( timeStr );
printf( "The current time is %s /n", timeStr);

--------實踐證明是正確的版本--------------------------------------------------------------
#include <iostream>
#include <ctime>
#include <cerrno>
 
int main()
{
     //Find the current time
     time_t curtime = time(0);
     
      //convert it to tm
      tm now=*localtime(&curtime);
    
     //BUFSIZ is standard macro that expands to a integer constant expression
     //that is greater then or equal to 256. It is the size of the stream buffer
     //used by setbuf()
     char dest[BUFSIZ]={0};
    
     //Format string determines the conversion specification's behaviour
     const char format[]="%A, %B %d %Y. The time is %X";
    
     //strftime - converts date and time to a string
     if (strftime(dest, sizeof(dest)-1, format, &now)>0)
       std::cout<<dest<<std::endl;
     else
       std::cerr<<"strftime failed. Errno code: "<<errno<<std::endl;
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
8.string to cstring

+++++++++++++++++++++++++++++++++++++++++++++++++++++
非Unicode下:
int 轉 CString:
CString.Format("%d",int);
...............................
string 轉 CString 
CString.format("%s", string.c_str()); 
用c_str()確實比data()要好. 
.......................................
char* 轉 CString 
CString.format("%s", char*); 
 CString strtest; 
 char * charpoint; 
 charpoint="give string a value"; 
 strtest=charpoint; //直接付值
.....................................................
CString 轉 int
 CString  ss="1212.12"; 
 int temp=atoi(ss); //atoi _atoi64或atol
...................................................................................................................................
9.在Unicode下的CString to double
CSting sTemp("123.567");
double dTemp = _wtof(sTemp.GetString());

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.