#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個(gè)參數(shù):第一個(gè)參數(shù)是要轉(zhuǎn)換的數(shù)字,第二個(gè)參數(shù)是目標(biāo)字符串,第三個(gè)參數(shù)是轉(zhuǎn)移數(shù)字時(shí)所用 的基數(shù)。在上例中,轉(zhuǎn)換基數(shù)為10。10:十進(jìn)制;2:二進(jìn)制……
于是想到了一個(gè)十進(jìn)制轉(zhuǎn)二進(jì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)換為二進(jìn)制的字符串,再把該字符串轉(zhuǎn)換為整數(shù)。
posted on 2006-10-12 00:59
beyonlin 閱讀(67443)
評(píng)論(14) 編輯 收藏 引用 所屬分類:
acm之路 、
C++之路