一、使用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 = ;
char c[];
itoa(n, c, );
cout << "2-> " << c << endl;
itoa(n, c, );
cout << "16-> " << c << endl;
itoa(n, c, );
cout << "10-> " << c << endl;
system("pause");
return ;
}

输出:

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 = ;
char c[];
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 ;
}

输出:

30
36
1E//注:这里是个特殊符号
24.677999
24.68
30-24.68

三、使用stringstream

Linux下编译通过的通用模板(int,double,char[]通过,推荐):

/*
convert other data to string
usage :
string str = m_toStr<int>(12345);
*/
template <class T> string m_toStr(T tmp)
{
stringstream ss;
ss << tmp;
return ss.str();
}

  

其他例子:

//-------------------------------------
//功能: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(stringstream可转更多类型)的更多相关文章

  1. int和string之间的转换

    #include<cstring> #include<algorithm> #include<stdio.h> #include<iostream> # ...

  2. int to string & string to int

    #include "stdafx.h" #include <string> #include <sstream> using namespace std; ...

  3. C++中int转string与string转int

    #include "stdafx.h" #include "string" #include "iostream" #include &qu ...

  4. P1980 计数问题(int,string,stringstream)

    题目描述 试计算在区间 1 到 n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1 到 11 中,即在 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 4 ...

  5. C++ 中 int 转string, 以及10进制转2进制

    感谢:http://blog.csdn.net/xiaofei2010/article/details/7434737 以及:http://www.cnblogs.com/nzbbody/p/3504 ...

  6. int and string

    int转string一.#include <sstream> int n = 0; std::stringstream ss; std::string str; ss<<n; ...

  7. C++ int与string的转化

    int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀,告诉编译器按照不同进制去解释.8进制 ...

  8. C++里的int 和string类型相互转换

    C++不像Java和C#一样在进行数据类型转换时直接调用一些类方法就可以了,使用起来很简单. 一个很简单的例子就是string str=“D:\\”+1+“.txt”;这在Java或者C#里面是可以自 ...

  9. C++ int转string

    一.使用atoi 说明: itoa(   int   value,   char   *string,   int   radix   );       第一个参数:你要转化的int;       第 ...

随机推荐

  1. eclipse+maven+tomcat构建web工程

    我们要利用Maven构建一个web应用,开发环境为eclipse+tomcat.构建过程如下: 1.工具准备 eclipse:版本为eclipse 4.2(Juno Service),maven插件的 ...

  2. 用ildasm/ilasm修改IL代码(操作步骤)

    在开发中遇到这样一个场景,需要修改一个dll文件(.NET程序集)中某些地方的类型名称,但没有源代码,只能修改IL代码. 操作步骤如下: 1. 运行ildasm ildasm是由微软提供的.NET程序 ...

  3. h5前端项目常见问题汇总

    原文作者:FrontEndZQ 原文链接:https://github.com/FrontEndZQ/HTML5-FAQ H5项目常见问题及注意事项 Meta基础知识: H5页面窗口自动调整到设备宽度 ...

  4. 028-B+树(一)

    B+ 树 这部分主要学习:什么是B+树? 了解了 B 树后再来了解下它的变形版:B+ 树,它比 B 树的查询性能更高. 一棵 B+ 树需要满足以下条件: 节点的子树数和关键字数相同(B 树是关键字数比 ...

  5. poj1434 Fill the Cisterns!

    地址:http://poj.org/problem?id=1434 题目:Fill the Cisterns! Fill the Cisterns! Time Limit: 5000MS   Memo ...

  6. python weekday()函数

    def weekday(self): """Return the day of the week as an integer, where Monday is 0 and ...

  7. springboot+mybatis项目自动生成

    springboot_data_access_demo基于rapid,根据自定义模版生成的基于mybatis+mysql的数据库访问示例项目.简单配置数据库信息,配置不同的生成策略生成可以直接运行访问 ...

  8. P1131 [ZJOI2007]时态同步(树形dp)

    P1131 [ZJOI2007]时态同步 设$f[i]$为与$i$与最远的点的距离 在dfs时每次更新的时候顺便统计一下长度,不同的话就改成最长的那条并更新答案 #include<iostrea ...

  9. JAVA_返回一个数值的相反数的几种方式.

    一个方法接收一个int类型值,需要返回它的相反数. 如传入1,返回-1 传入-22,返回22 最简单的方式是return 0-number; 还有其他方式: public class Kata { p ...

  10. class文件的结构

    任何一个class文件都对应着唯一一个类或接口的定义信息,但反过来说,类或接口的定义信息并不一定都在文件里(比如类或接口也可以通过类加载器直接生成) class文件是一组以8位字节为基础单位的二进制流 ...