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  
請按任意鍵繼續. . .

注意:

使用stringstream時的清空操作

      在C++中可以使用stringstream來很方便的進行類型轉換,字符串串接,不過注意重復使用同一個stringstream對象時要先繼續清空,而清空很容易想到是clear方法,而在stringstream中這個方法實際上是清空stringstream的狀態(比如出錯等),真正清空內容需要使用.str(“”)方法。

四、其它

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/

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM