#include<cstdlib>
#include<cstdio>
int main()
{
int num = 10;
char str[100];
itoa(num, str, 2);
printf("%s\n", str);
return 0;
}
itoa()函數(shù)有3個參數(shù):第一個參數(shù)是要轉(zhuǎn)換的數(shù)字,第二個參數(shù)是目標字符串,第三個參數(shù)是轉(zhuǎn)移數(shù)字時所用 的基數(shù)。在上例中,轉(zhuǎn)換基數(shù)為10。10:十進制;2:二進制……
于是想到了一個十進制轉(zhuǎn)二進制的方法:
#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轉(zhuǎn)換為二進制的字符串,再把該字符串轉(zhuǎn)換為整數(shù)。
posted on 2006-10-12 00:59
beyonlin 閱讀(67444)
評論(14) 編輯 收藏 引用 所屬分類:
acm之路 、
C++之路