close

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);

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

--------實踐証明是正確的版本--------------------------------------------------------------

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

4. double ,int to string
#include <sstream>
using namespace std;

#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;


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

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

+++++++++++++++++++++++++++++++++++++++++++++++++++++
非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());

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 );

一大哥買了W12 6.0的輝騰,引發了以下的小故事! 某日,大哥進停車場,正在自動泊車中,筦理員沖他喊道:“喂,開PASSAT的小心點,別把邊上的新寶馬320給撞了,你賠不起!”大哥一 怒吼到“老子的車夠買他5輛了!" 某日,大哥進加油站,一不留神,加油小妹的93號槍就對准開加了,大哥汗都下來了,吼到“老子是要加 97的,誰讓你自作主張加93了!” 小妹好心的回說“大哥,PASSAT加93的絕對沒事,我每天都加多少PASSAT,93的省錢,不是奔馳寶馬沒必要加97~” 某日,大 哥去夜店泡   妞,看中一正妹後,相約一並外出霄夜,兼後半場,正妹看大哥氣質不凡,跟他走向停車的地方,一見大哥的輝騰,正妹扭頭就走,並說到“開桑塔納還 好意思約我吃霄夜,早知道跟剛才開凱美瑞的走了……” 某日,大哥宴請一ZF高官,開車到其傢接他,大哥恭敬的把車門打開,高官走近一 看,說到“我還是讓司機把A6開出來吧” 某日,大哥到一處接朋友,此小區門口黑車眾多,大哥在車上等朋友之際聽到黑司機們議論,“這麼好 的車也出來拉活啊”,另一位說到“可能單位的吧,不過PASSAT怎麼也得百公裏8個油吧”大哥搖下車窗怒吼之“老子百公裏得17個油!!!” 某 日,大哥一朋友之重要朋友結婚,要借一重量級頭車,大哥厚道之人,雖愛車之心無比,也大方出借,事後朋友還車,大哥自豪問到“怎麼樣,讓你在朋友面前長臉 吧?”朋友歎道“別提了,朋友非說這是一老款的PASSAT太不拉風,最後又花錢租了一輛噹前的紅色跑車噹婚車,你的車就排到婚車隊最後一個專門接送上了 年紀的老人傢和兒童了。” 某日,大哥停完車正往外准備走的時候,聽見身後兩個年輕人的對話,一人說到“快看,哇塞,輝騰啊!”,大哥心中 一片舒坦,終於有識貨之人,正爽之際,聽到另一年輕人說“操,真有SB買它啊!”

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();

5.char* to int, double ,long

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

注:GetBuffer()後一定要ReleaseBuffer(),否則就沒有釋放緩沖區所佔的空間.
++++++++++++++++++++++++++++++++++++++++++++++++++++

VC中int,char,char*,const char*,string等類型轉換 1. char* to string
string s(char *); 
注:在不是初始化的地方最好用assign().
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2. string to const char*
string a="strte";
const char* r=a.c_str(),mosnter beats ibeats;
注意是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();


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

    else 
       std::cerr<<"strftime failed. Errno code: "<<errno<<std::endl;
}

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

arrow
arrow
    全站熱搜

    hadatemegumi 發表在 痞客邦 留言(0) 人氣()