随笔 - 62  文章 - 70  trackbacks - 0
<2007年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(3)

随笔分类(66)

随笔档案(62)

文章分类(31)

文章档案(31)

友情链接

最新随笔

积分与排名

  • 积分 - 38418
  • 排名 - 62

最新评论

阅读排行榜

评论排行榜

#include<cstdlib>
#include<cstdio>
int main() { int num = 10; char str[100]; itoa(num, str, 2); printf("%s\n", str); return 0; }
itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是目标字符串,第三个参数是转移数字时所用 的基数。在上例中,转换基数为10。10:十进制;2:二进制……
于是想到了一个十进制转二进制的方法:
#include<cstdlib>
#include<cstdio>
int main() { int num = 10; char str[100]; int n = atoi(itoa(num, str, 2)); printf("%d\n",n); return 0; }
先把num转换为二进制的字符串,再把该字符串转换为整数。
posted on 2006-10-12 00:59 beyonlin 阅读(13808) 评论(7)  编辑 收藏 引用 所属分类: acm之路C++之路

FeedBack:
# re: itoa函数 2006-10-23 23:45 Asp
感觉很容易越界吧……  回复  更多评论
  
# re: itoa函数 2006-10-24 00:33 beyonlin
@Asp
只是介绍一下STL函数的用法。
那个str[100]只是一个例子~~  回复  更多评论
  
# re: itoa函数 2007-07-11 08:52 小文
第二个参数用string类型,可以吗?  回复  更多评论
  
# re: itoa函数 2007-07-11 09:49 AlanTop
另一个10进制转二进制的方法
/*
* 引用自 http://www.cppblog.com/alantop/archive/2007/07/11/27845.html
*/
// compile with: /EHsc
#include
#include

int main( )
{
using namespace std;
bitset<20> b1 ( 10 );
cout << "The set of bits in bitset<5> b1( 10 ) is: ( "
<< b1 << " )." << endl;
}
  回复  更多评论
  
# re: itoa函数 2007-07-12 18:02 beyonlin
@小文
char* itoa(int, char*, int)
第二个参数是char*,不能用string  回复  更多评论
  
# re: itoa函数 2007-12-19 16:12 sakar2003
itoa只是WINDOWS下面才有的函数吧,UNIX下面是没有itoa的,跨平台还是sprintf好  回复  更多评论
  
# re: itoa函数 2008-08-01 11:33 sheena
怎么第二个函数无法编译呢。?
int n = atoi(itoa(num, str, 2));

n是不是还是应该是当初num 10 的值啊  回复  更多评论
  

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航: