int转string

来源:互联网 发布:睡过十几个女同学 知乎 编辑:程序博客网 时间:2024/05/23 17:49

一、使用atoi

说明:

itoa(   int   value,   char   *string,   int   radix   );   
    第一个参数:你要转化的int;   
    第二个参数:转化后的char*;   
    第三个参数:你要转化的进制;  

举例:

[cpp] view plaincopy
  1. //-------------------------------------  
  2. //功能:C++ int 转 string (使用atoi)   
  3. //环境:VS2005  
  4. //-------------------------------------  
  5. #include "stdafx.h"  
  6. #include <iostream>  
  7. using namespace std;  
  8. int main(int argc, char* argv[])  
  9. {  
  10.     int n = 30;    
  11.     char c[10];  
  12.       
  13.     itoa(n, c, 2);  
  14.     cout << "2-> " << c << endl;  
  15.     itoa(n, c, 10);  
  16.     cout << "16-> " <<  c << endl;  
  17.     itoa(n, c, 16);   
  18.     cout << "10-> " <<  c << endl;  
  19.     system("pause");  
  20.     return 0;  
  21. }  

输出:

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-sharp] view plaincopy
  1. //-------------------------------------  
  2. //功能:C++ int 转 string (使用sprintf)   
  3. //环境:VS2005  
  4. //-------------------------------------  
  5. #include "stdafx.h"  
  6. #include <iostream>  
  7. #include <string>  
  8. using namespace std;  
  9. int main()  
  10. {  
  11.     int n = 30;  
  12.     char c[20];  
  13.     sprintf(c, "%d", n);      
  14.     cout << c << endl;  
  15.     sprintf(c, "%o", n);      
  16.     cout << c << endl;  
  17.     sprintf(c, "%X", n);      
  18.     cout << c << endl;  
  19.     sprintf(c, "%c", n);      
  20.     cout << c << endl;  
  21.     float f = 24.678;  
  22.     sprintf(c, "%f", f);      
  23.     cout << c << endl;  
  24.     sprintf(c, "%.2f", f);      
  25.     cout << c << endl;  
  26.     sprintf(c, "%d-%.2f", n, f);  
  27.     cout << c << endl;  
  28.     system("pause");  
  29.     return 0;  
  30. }  

输出:

30
36
1E
//注:这里是个特殊符号
24.677999
24.68
30-24.68
请按任意键继续. . .

三、使用stringstream

举例:

[cpp] view plaincopy
  1. //-------------------------------------  
  2. //功能:C++ int 转 string (使用stringstream)   
  3. //环境:VS2005  
  4. //-------------------------------------  
  5. #include "stdafx.h"  
  6. #include <iostream>  
  7. #include <string>  
  8. #include <sstream>  
  9. using namespace std;  
  10. int main()  
  11. {  
  12.     stringstream strStream;  
  13.     int a = 100;  
  14.     float f = 23.5566;  
  15.     strStream << a << "----"<< f ;  
  16.     string s = strStream.str();  
  17.     cout << s << endl;  
  18.     system("pause");  
  19.     return 0;  
  20. }  

输出:

100----23.5566
请按任意键继续. . .

四、其它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/

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

原创粉丝点击