c++ int 转 string
来源:互联网 发布:linux vps speedtest 编辑:程序博客网 时间:2024/06/04 18:57
一、使用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-> 1111016-> 3010-> 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;
}
输出:
30361E//注:这里是个特殊符号
24.67799924.6830-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/
//-----------------------------------------------------------------------------------
- C++:int转string
- c++int转string
- C++int转string
- C/C++ string转int
- c int 转string 和string 转int
- :string转int,double? (C/C++)
- string转int,double? (C/C++)
- 【C++】int转string,double转string方法,string转int,string转double方法
- 【C++】int与string的互转
- C#,int转成string,string转成int
- C#,int转成string,string转成int
- C#,int转成string,string转成int
- C#,int转成string,string转成int
- C#,int转成string,string转成int
- C#,int转成string,string转成int
- C#,int转成string,string转成int
- C++: int to string
- c#:int--string
- tcpflow命令使用总结
- 面试题——SSH优缺点
- 我大学剩下一年半的生活(五)——pythonchallenge之level4
- Collection/List/Set
- POJ 2028 When Can We Meet?(我的水题之路——边输入,边搜索)
- c++ int 转 string
- 赶集交友群发返回信息
- 第4章 编写正确的程序
- [观点]缺乏想象力的程序员
- 浅谈三层架构
- 软件产品细分之我见
- ACM小技巧
- 解决 IE6 下 img 多余 5 个像素的空白
- 学会鼓励