C++ int类型转换string类型
2015-08-22 13:40
531 查看
C++中不像C#或Java中能直接使用字符串加法将 int类型转换为string类型。C++中进行这样的类型转换需要一些额外的函数。
一、C++的int转string
#方法一: 使用itoa函数: char * itoa ( int value, char * str, int base );
说明:Convert integer to string (non-standard function)
参数:
·value : Value to be converted to a string.
·str : Array in memory where to store the resulting null-terminated string.
·base : Numerical base used to represent the value as a string, between2 and36, where10 means decimal base, 16hexadecimal,8 octal, and2 binary.
Demo:
[cpp] view
plaincopy
#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 << "10-> " << c << endl;
//十六进制转换
itoa(n, c, 16);
cout << "16-> " << c << endl;
system("pause");
return 0;
}
输出结果:
#方法二: 使用sprintf: int sprintf ( char * str, const char * format, ... );
参数说明:
Demo:
[cpp] view
plaincopy
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n = 30;
char c[20]; //char *c;
//%d十进制
sprintf(c, "%d", n);
cout << c << endl;
//%o八进制
sprintf(c, "%o", n);
cout << c << endl;
//%X大写十六进制
sprintf(c, "%X", n);
cout << c << endl;
//%cACSII字元
sprintf(c, "%c", n);
cout << c << endl;
//%f浮点数转换
float f = 24.678;
sprintf(c, "%f", f);
cout << c << endl;
//%.2f"保留小数点后面两位
sprintf(c, "%.2f", f);
cout << c << endl;
//转换两个数
sprintf(c, "%d-%.2f", n, f);
cout << c << endl;
system("pause");
return 0;
}
输出结果:
#方法三:使用stringstream
Input/output string stream class: stringstream provides an interface to manipulate strings as if they were input/output streams.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/08/a87762836f0f9bc73aa8bfc1f67aa5e7.gif)
Demo:
[cpp] view
plaincopy
#include <iostream>
#include <string>
#include <sstream> //引入stringstream头文件
using namespace std;
int main()
{
stringstream strStream;
int a = 100;
float f = 23.5566;
//int、float类型都可以塞到stringstream中
strStream << a << "----"<< f ;
string s = strStream.str();
cout << s << endl;
system("pause");
return 0;
}
输出结果:
#四、其他
1.sprintf可能引起缓冲区溢出,可以考虑使用 snprintf 或者非标准的 asprintf
2.如果是mfc程序,可以使用 CString::Format
3.如果使用boost,则可以直接使用: string s = boost::lexical_cast <string>(a);
4.atoi 也是不可移植的。
一、C++的int转string
#方法一: 使用itoa函数: char * itoa ( int value, char * str, int base );
说明:Convert integer to string (non-standard function)
参数:
·value : Value to be converted to a string.
·str : Array in memory where to store the resulting null-terminated string.
·base : Numerical base used to represent the value as a string, between2 and36, where10 means decimal base, 16hexadecimal,8 octal, and2 binary.
Demo:
[cpp] view
plaincopy
#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 << "10-> " << c << endl;
//十六进制转换
itoa(n, c, 16);
cout << "16-> " << c << endl;
system("pause");
return 0;
}
输出结果:
2-> 11110 10-> 30 16-> 1e 请按任意键继续. . . |
参数说明:
% 印出百分比符号,不转换。 b 整数转成二进位。 c 整数转成对应的 ASCII 字元。 d 整数转成十进位。 f 倍精确度数字转成浮点数。 o 整数转成八进位。 s 整数转成字串。 x 整数转成小写十六进位。 X 整数转成大写十六进位。 |
[cpp] view
plaincopy
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n = 30;
char c[20]; //char *c;
//%d十进制
sprintf(c, "%d", n);
cout << c << endl;
//%o八进制
sprintf(c, "%o", n);
cout << c << endl;
//%X大写十六进制
sprintf(c, "%X", n);
cout << c << endl;
//%cACSII字元
sprintf(c, "%c", n);
cout << c << endl;
//%f浮点数转换
float f = 24.678;
sprintf(c, "%f", f);
cout << c << endl;
//%.2f"保留小数点后面两位
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 请按任意键继续. . . |
Input/output string stream class: stringstream provides an interface to manipulate strings as if they were input/output streams.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/08/a87762836f0f9bc73aa8bfc1f67aa5e7.gif)
Demo:
[cpp] view
plaincopy
#include <iostream>
#include <string>
#include <sstream> //引入stringstream头文件
using namespace std;
int main()
{
stringstream strStream;
int a = 100;
float f = 23.5566;
//int、float类型都可以塞到stringstream中
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 也是不可移植的。
相关文章推荐
- C++常见面试
- C++面试常见题目问与答(汇总一)
- POJ C++程序设计 编程题#4:魔兽世界之一:备战
- 图解C++
- C#在C++编写的DLL中注册函数并回调
- C语言快排
- C++ Primer 5e chapter 12
- C++ Primer 5e chapter 11
- C++ 与 c# 模块 之间相互调用
- 栈的应用2——超级计算器(中缀与后缀表达式)C语言
- 栈的应用1——超级计算器(中缀与后缀表达式)C语言
- C++ 编程个人总结
- C++中 explicit的用法
- GCC在C语言中内嵌汇编 asm __volatile__ (2)
- C语言编程技巧----如何定义全局变量----->提高代码可移植性
- C和C++中的文件读入写出区别
- C++联合
- Effective C++ 条款8 别让异常逃离析构函数
- 为何C语言(的函数调用)需要堆栈,而汇编语言却不需要堆栈
- C++模板的理解与使用