int转string
来源:互联网 发布:睡过十几个女同学 知乎 编辑:程序博客网 时间:2024/05/23 17:49
一、使用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请按任意键继续. . .
四、其它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/
//-----------------------------------------------------------------------------------
- [转]int转string string转int
- int转string string转int
- c# int 转 string string 转 int
- int转string,string转int
- c++ string转int,int转string
- int转string/ string 转Int
- String转int转String
- C++ int转string string转int int转二进制
- String 转 int
- string 转 int 函数
- int 转 string
- string 转 int
- string 转 int
- int转string
- int 转 string
- int转string
- String 转int
- c++ int 转 string
- C#实现局域网UDP广播
- 高级屏蔽优酷广告(终极利器)
- JS 判断单选框(radio)是否被选中
- ubuntu 11.10 3D桌面特效及其窗口特效设置
- 随想录(锁的来由和使用)
- int转string
- 成功人士必备~Microsoft SQL Server 2008 安装图解(Windows 7)
- 80、90后员工管理八项注意
- Windows7 Android 开发环境搭建
- 开启Ubuntu下lamp的php错误提示
- 基于视觉信息的网页分块算法(VIPS)
- 找出单链表的第一个公共节点
- readLine方法,方便于对文本数据的获取
- 一个简单的linux crackme的逆向