c++ int 轉 string

一、使用atoi

說明:

itoa(   int   value,   char   *string,   int   radix   );   
    第一個參數:你要轉化的int;   
    第二個參數:轉化後的char*;   
    第三個參數:你要轉化的進制;  

舉例:

//-------------------------------------  
//功能:C++ int 轉 string (使用atoi)   
//環境:VS2005  
//-------------------------------------  
#include "stdafx.h"  
#include <iostream>  
using namespace std;  
int main(int argc, char* argv[])  
{  
    int n = 30;    
    char c[10];  
      
    itoa(n, c, 2);  
    cout << "2-> " << c << endl;  
    itoa(n, c, 10);  
    cout << "16-> " <<  c << endl;  
    itoa(n, c, 16);   
    cout << "10-> " <<  c << endl;  
    system("pause");  
    return 0;  
}

輸出:

2-> 11110  
16-> 30  
10-> 1e  
請按任意鍵繼續. . .


二、使用sprintf

頭文件 #include<stdio.h>

語法: int sprintf(string format, mixed [args]...);

返回值:字符串長度(strlen)

轉換字符

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% 印出百分比符號,不轉換。

b 整數轉成二進位。

c 整數轉成對應的 ASCII 字元。

d 整數轉成十進位。

f 倍精確度數字轉成浮點數。

o 整數轉成八進位。

s 整數轉成字串。

x 整數轉成小寫十六進位。

X 整數轉成大寫十六進位。

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 
舉例:

//-------------------------------------  
//功能:C++ int 轉 string (使用sprintf)   
//環境:VS2005  
//-------------------------------------  
#include "stdafx.h"  
#include <iostream>  
#include <string>  
using namespace std;  
int main()  
{  
    int n = 30;  
    char c[20];  
    sprintf(c, "%d", n);      
    cout << c << endl;  
    sprintf(c, "%o", n);      
    cout << c << endl;  
    sprintf(c, "%X", n);      
    cout << c << endl;  
    sprintf(c, "%c", n);      
    cout << c << endl;  
    float f = 24.678;  
    sprintf(c, "%f", f);      
    cout << c << endl;  
    sprintf(c, "%.2f", f);      
    cout << c << endl;  
    sprintf(c, "%d-%.2f", n, f);  
    cout << c << endl;  
    system("pause");  
    return 0;  
}  

輸出:

30  
36  
1E  
//注:這裏是個特殊符號  
24.677999  
24.68  
30-24.68  
請按任意鍵繼續. . .

三、使用stringstream

舉例:

//-------------------------------------  
//功能:C++ int 轉 string (使用stringstream)   
//環境:VS2005  
//-------------------------------------  
#include "stdafx.h"  
#include <iostream>  
#include <string>  
#include <sstream>  
using namespace std;  
int main()  
{  
    stringstream strStream;  
    int a = 100;  
    float f = 23.5566;  
    strStream << a << "----"<< f ;  
    string s = strStream.str();  
    cout << s << endl;  
    system("pause");  
    return 0;  
}  

輸出:

100----23.5566  
請按任意鍵繼續. . .

四、其它

1.sprintf可能引起緩衝區溢出,可以考慮使用 snprintf 或者非標準的 asprintf

2.如果是mfc程序,可以使用 CString::Format

3.如果使用boost,則可以直接使用: string s = boost::lexical_cast <string>(a);

4.atoi 也是不可移植的。

五、其它NB方法

//-----------------------------------------------------------------------------------

// 參考引用 :

// http://baike.baidu.com/view/982195.htm?fr=ala0_1_1

// http://baike.baidu.com/view/1295144.htm?fr=ala0_1

// http://pppboy.blog.163.com/blog/static/3020379620085511954382/

//-----------------------------------------------------------------------------------

發佈了72 篇原創文章 · 獲贊 4 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章