--
| Jianjun Kong | www.kongove.cn
| Xi'an Institute of Post & Telecommunications
我把上边网页上第一个方法修改了一下,测试没问题。大家再看看
#include <stdio.h>
int flag=1;//当未输出1时flag为1
void ten2two(int number)
{
int reminder;
reminder = number%2;
number = number/2;
if (number <= 1)
{
if(flag==1 && number==0)
flag=0;
else
printf("%d",number);
}
else
ten2two(number);
printf("%d", reminder);
}
int main(void)
{
int inumber;
printf("Please input int number:\n");
scanf("%d", &inumber);
printf("binary:");
ten2two(inumber);
printf("\n");
return 0;
}
关于C/C++打印二进制这里有很多方法
http://www.52unix.net/software/p613/A61375445.shtml
我试了一下,有些还有问题,有没有比较正规的解决方法。
能不能具体提示一下?
On Thu, Apr 17, 2008 at 11:16:45PM +0800, 李磊 wrote:
>
>
> 在08-4-17,Jianjun Kong <kongj...@gmail.com> 写道:
>
> 关于C/C++打印二进制这里有很多方法
> http://www.52unix.net/software/p613/A61375445.shtml
> 我试了一下,有些还有问题,有没有比较正规的解决方法。
>
> 进制转换问题吗?
> 用栈吧。我们正好才学过。
能不能具体提示一下?
--
********************************
http://www.xiao-chun.cn
********************************
On Thu, Apr 17, 2008 at 11:16:45PM +0800, 李磊 wrote:
>
>
> 在08-4-17,Jianjun Kong <kongj...@gmail.com> 写道:
>
> 关于C/C++打印二进制这里有很多方法
> http://www.52unix.net/software/p613/A61375445.shtml
> 我试了一下,有些还有问题,有没有比较正规的解决方法。
>
> 进制转换问题吗?
> 用栈吧。我们正好才学过。
能不能具体提示一下?
数字过大时就输出:
011111111111111111111
我的机器是64位的
int最大可表示 2^31-1
这里数组应为 31个元素
把这里定义成unsigned类型的,能转化的数字范围会更大。
> int n,i=0;
> printf("please input the value of n: ");
> scanf("%d",&n);
> while(n){
> a[i++]=n%2;
> n=n/2;
> }
> for(i--; i >= 0; i--){
> printf("%d",a[i]);
> }
> printf("\n");
> return 0;
> }
Thanks
On Fri, Apr 18, 2008 at 01:05:23PM +0800, 李磊 wrote:
>
>
> 在08-4-17,Jianjun Kong <kongj...@gmail.com> 写道:
>
> On Thu, Apr 17, 2008 at 11:16:45PM +0800, 李磊 wrote:
> >
> >
> > 在08-4-17,Jianjun Kong <kongj...@gmail.com> 写道:
> >
> > 关于C/C++打印二进制这里有很多方法
> > http://www.52unix.net/software/p613/A61375445.shtml
> > 我试了一下,有些还有问题,有没有比较正规的解决方法。
> >
> > 进制转换问题吗?
> > 用栈吧。我们正好才学过。
>
> 能不能具体提示一下?
>
>
> #include <stdio.h>
> int main(void)
> {
> int a[20];
数字过大时就输出:
011111111111111111111
我的机器是64位的
int最大可表示 2^31-1
这里数组应为 31个元素
把这里定义成unsigned类型的,能转化的数字范围会更大。
关于C/C++打印二进制这里有很多方法
http://www.52unix.net/software/p613/A61375445.shtml
我试了一下,有些还有问题,有没有比较正规的解决方法。
这个说法正确吗?你的编译器是什么?标准规定的是long等于机器字长,short小于long,而int只要不小于short也不大于long就可以了。而且事实上,GCC和现在VC++在我的电脑上,int都是32bit的!
#include <stdio.h>
#include <stdint.h>
void Printf_Binary(int buf)
{
uint32_t size = sizeof(int)*8;
char tmp[sizeof(int)*8+1];
char cnt;
char flag='0'; /* 当遇到第一个非0数是变为'2'*/
tmp[size] = '\0';
for (cnt=0; cnt<size; cnt++)
{
tmp[size-cnt-1] = ((buf >> cnt) & 0x1)+'0';
}
for(cnt=0; cnt<size; cnt++)
if(tmp[cnt]!=flag)
{
flag='2';
printf("%c",tmp[cnt]);
}
printf("\n");
}
int main()
{
int i;
scanf("%i",&i);
Printf_Binary(i);
return 0;
}
--
| Olympic of world,Tibet of China!
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tmp[size] = '\0';
for (cnt=0; cnt<size; cnt++)
{
tmp[size-cnt-1] = ((buf >> cnt) & 0x1)+'0';
}
p=strstr(tmp,"1");
printf("%s\n",p);
}
int main()
{
int i;
printf("Input a integer number:");
scanf("%d",&i);
Printf_Binary(i);
return 0;
}
<snip>
> 我觉得出于程序的健壮性考虑,我觉得应该先判断下输入的字符。如果为数字,一切ok;如果为其它符号则报错退出;
> #include<stdio.h>
> #include<stdint.h>
> #include<string.h>
> #include<math.h>
> +#include<ctype.h>
> void Printf_Binary_digit(int buf)
> {
> unsigned int size = sizeof(int)*8;
>
> char tmp[sizeof(int)*8+1];
> int cnt;
> char *p;
>
> tmp[size] = '\0';
>
> for (cnt=0; cnt<size; cnt++)
> {
> tmp[size-cnt-1] = ((buf >> cnt) & 0x1)+'0';
> }
>
> p=strstr(tmp,"1");
> printf("%s\n",p!=NULL?p:"");
>
> }
> }
>
> int main()
> {
> int i;
> + printf("the limit ofsize is 2^32-1=%f\n",exp2(32)-1);
用: ~0U 不是更简单??
>
> printf("Input a integer number:");
> scanf("%d",&i);
> + if( isdigit(i))
你没有理解isdigit的作用。
> + Printf_Binary_digit(i);
> + else
> + { printf("wrong input,please check it again ^|^\n");
> + return 1;
> + }
> return 0;
> }
>
Thanks.
---
Hi, I'm a .signature virus, please copy/paste me to help me spread
all over the world.