[转]int转string string转int

string转int 相应为

int i = atoi(str.c_str());

 

 int转string
一、使用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-> 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 = 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;
}

输出:

30
36
1E
//注:这里是个特殊符号
24.677999
24.68
30-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/

//-----------------------------------------------------------------------------------

 

string轉int

int i = atoi(a.c_str());

 

轉自 http://pppboy.blog.163.com/blog/static/302037962010378296766/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值