C++ int转string(stringstream可转更多类型)
一、使用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可转更多类型)的更多相关文章
- int和string之间的转换
#include<cstring> #include<algorithm> #include<stdio.h> #include<iostream> # ...
- int to string & string to int
#include "stdafx.h" #include <string> #include <sstream> using namespace std; ...
- C++中int转string与string转int
#include "stdafx.h" #include "string" #include "iostream" #include &qu ...
- 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 ...
- C++ 中 int 转string, 以及10进制转2进制
感谢:http://blog.csdn.net/xiaofei2010/article/details/7434737 以及:http://www.cnblogs.com/nzbbody/p/3504 ...
- int and string
int转string一.#include <sstream> int n = 0; std::stringstream ss; std::string str; ss<<n; ...
- C++ int与string的转化
int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀,告诉编译器按照不同进制去解释.8进制 ...
- C++里的int 和string类型相互转换
C++不像Java和C#一样在进行数据类型转换时直接调用一些类方法就可以了,使用起来很简单. 一个很简单的例子就是string str=“D:\\”+1+“.txt”;这在Java或者C#里面是可以自 ...
- C++ int转string
一.使用atoi 说明: itoa( int value, char *string, int radix ); 第一个参数:你要转化的int; 第 ...
随机推荐
- python技巧总结之set、日志、rsa加密
一.日志模块logging模块调用 1.日志模块使用原理 #!/usr/bin/python # -*- coding:utf-8 -*- import logging # 方式一: "&q ...
- IETF国标查询
IETF官网 https://www.ietf.org/ rfc国标官网 https://www.ietf.org/standards/rfcs/ rfc国标查询 https://www.rfc-ed ...
- Apache配置虚拟主机后让其他电脑访问
关于Apache配置虚拟主机后在局域网中让其他电脑访问 #test1# NameVirtualHost *:80 ServerName www.t1.com Document ...
- L1正则与L2正则
L1正则是权值的绝对值之和,重点在于可以稀疏化,使得部分权值等于零. L1正则的含义是 ∥w∥≤c,如下图就可以解释为什么会出现权值为零的情况. L1正则在梯度下降的时候不可以直接求导,可以有以下几种 ...
- [华为]输入n个整数,输出其中最小的k个
链接:https://www.nowcoder.com/questionTerminal/69ef2267aafd4d52b250a272fd27052c来源:牛客网 输入n个整数,输出其中最小的k个 ...
- 72. Edit Distance(编辑距离 动态规划)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- VS2010/MFC编程入门之十三(对话框:属性页对话框及相关类的介绍)
前面讲了模态对话框和非模态对话框,本节开始鸡啄米讲一种特殊的对话框--属性页对话框.另外,本套教程所讲大部分对VC++各个版本均可适用或者稍作修改即可,但考虑到终究还是基于VS2010版本的,所以将& ...
- Good Bye 2018 Solution
A. New Year and the Christmas Ornament 签到. #include <bits/stdc++.h> using namespace std; int a ...
- presto-cli通过hive查询hdfs
1. 启动hive metastore 2. 启动hive thrift接口 参考:http://www.cnblogs.com/kisf/p/7497261.html 3. 下载presto se ...
- CPA定律——一致性,可用性和分区容错性
按照美国著名科学家 Eric Brewer 在 2000 年提出的理论,当技术架构从集中式架构向分布式架构演进,会遇到 “CAP 定律”的瓶颈. CAP 说明一个数据处理系统不能同时满足一致性,可用性 ...