C/C++打印二进制

76 views
Skip to first unread message

Jianjun Kong

unread,
Apr 17, 2008, 7:43:22 AM4/17/08
to 西邮Linux兴趣小组
关于C/C++打印二进制这里有很多方法
http://www.52unix.net/software/p613/A61375445.shtml
我试了一下,有些还有问题,有没有比较正规的解决方法。

--
| Jianjun Kong | www.kongove.cn
| Xi'an Institute of Post & Telecommunications

Jianjun Kong

unread,
Apr 17, 2008, 8:01:46 AM4/17/08
to 西邮Linux兴趣小组
On Thu, Apr 17, 2008 at 10:43:22PM +1100, Jianjun Kong wrote:
> 关于C/C++打印二进制这里有很多方法
> http://www.52unix.net/software/p613/A61375445.shtml
> 我试了一下,有些还有问题,有没有比较正规的解决方法。

我把上边网页上第一个方法修改了一下,测试没问题。大家再看看

#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;
}

李磊

unread,
Apr 17, 2008, 11:16:45 AM4/17/08
to Jianjun Kong, 西邮Linux兴趣小组


在08-4-17,Jianjun Kong <kongj...@gmail.com> 写道:
关于C/C++打印二进制这里有很多方法
http://www.52unix.net/software/p613/A61375445.shtml
我试了一下,有些还有问题,有没有比较正规的解决方法。
 
进制转换问题吗?
用栈吧。我们正好才学过。

Jianjun Kong

unread,
Apr 17, 2008, 8:24:28 AM4/17/08
to 李磊, 西邮Linux兴趣小组
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
> 我试了一下,有些还有问题,有没有比较正规的解决方法。
>
> 进制转换问题吗?
> 用栈吧。我们正好才学过。

能不能具体提示一下?

Xiao Chun

unread,
Apr 18, 2008, 12:25:45 AM4/18/08
to Jianjun Kong, 李磊, 西邮Linux兴趣小组
在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
>     我试了一下,有些还有问题,有没有比较正规的解决方法。
>
> 进制转换问题吗?
> 用栈吧。我们正好才学过。


能不能具体提示一下?

完全模拟手工转换过程,将结果放在栈中。



--
********************************
  http://www.xiao-chun.cn  
********************************

李磊

unread,
Apr 18, 2008, 1:05:23 AM4/18/08
to Jianjun Kong, 西邮Linux兴趣小组
在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];
 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]);

Jianjun Kong

unread,
Apr 17, 2008, 10:50:50 PM4/17/08
to 李磊, 西邮Linux兴趣小组
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类型的,能转化的数字范围会更大。


> 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

李磊

unread,
Apr 18, 2008, 2:25:21 AM4/18/08
to Jianjun Kong, 西邮Linux兴趣小组


在08-4-18,Jianjun Kong <kongj...@gmail.com> 写道:
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
 
那32位的最大是多少呢?

这里数组应为 31个元素

把这里定义成unsigned类型的,能转化的数字范围会更大。
 
知道了。谢谢。

Jianjun Kong

unread,
Apr 18, 2008, 2:24:04 AM4/18/08
to 李磊, 西邮Linux兴趣小组
On Fri, Apr 18, 2008 at 02:25:21PM +0800, 李磊 wrote:
<snip>
> 数字过大时就输出:
> 011111111111111111111
>
> 我的机器是64位的
> int最大可表示 2^31-1
>
> 那32位的最大是多少呢?
32位机子上int是16bit
最大可表示2^15-1

WANG Cong

unread,
Apr 18, 2008, 6:19:15 AM4/18/08
to kongj...@gmail.com, lile...@gmail.com, xiyou...@googlegroups.com
From: Jianjun Kong <kongj...@gmail.com>
Date: Fri, 18 Apr 2008 17:24:04 +1100
Wrong.

GHost

unread,
Apr 18, 2008, 7:21:58 AM4/18/08
to WANG Cong, kongj...@gmail.com, lile...@gmail.com, xiyou...@googlegroups.com
在 08-4-18,WANG Cong<xiyou.w...@gmail.com> 写道:
<limit.h>中定义了一个INT_MAX值.

Xiao Chun

unread,
Apr 18, 2008, 7:30:40 AM4/18/08
to GHost, WANG Cong, kongj...@gmail.com, lile...@gmail.com, xiyou...@googlegroups.com


在08-4-18,GHost <aurt...@gmail.com> 写道:

 INT_MAX的大小是不是与实现有关?

GHost

unread,
Apr 18, 2008, 7:42:52 AM4/18/08
to Xiao Chun, WANG Cong, kongj...@gmail.com, lile...@gmail.com, xiyou...@googlegroups.com
在 08-4-18,Xiao Chun<xiaoch...@gmail.com> 写道:

理论上讲,确实与实现有关如此.
所以在<limit.h>中针对32,64位都定义了基本类型的大小.

vvoody

unread,
Apr 18, 2008, 7:51:57 AM4/18/08
to Jianjun Kong, 西邮Linux兴趣小组
On 4/17/08, Jianjun Kong <kongj...@gmail.com> wrote:
关于C/C++打印二进制这里有很多方法
http://www.52unix.net/software/p613/A61375445.shtml
我试了一下,有些还有问题,有没有比较正规的解决方法。



#include <stdio.h>

int dec2bin(int dec)
{
return (dec != 1) && !dec2bin( dec / 2 ) || printf("%d", dec & 1);
}

int main(void)
{
int dec;
scanf("%d", &dec);
dec2bin(dec);
printf("\n");
// printf("%d\n", dec2bin(dec));
return 0;
}

最近刚写的这个,WANG Cong 看了很眼熟吧。

--
Free as freedom, slack as Slackware.
vvoody

WANG Cong

unread,
Apr 18, 2008, 10:44:10 AM4/18/08
to xiaoch...@gmail.com, aurt...@gmail.com, kongj...@gmail.com, lile...@gmail.com, xiyou...@googlegroups.com
From: "Xiao Chun" <xiaoch...@gmail.com>
Date: Fri, 18 Apr 2008 19:30:40 +0800
当然。标准只规定int精度__至少__达到16bit就可以了,而实际中多数实现都是
32bit。当然了,windows上的某个实现又是异类,我们见怪不怪了,直接忽略!
;-)

WANG Cong

unread,
Apr 18, 2008, 11:23:19 AM4/18/08
to wxj....@gmail.com, kongj...@gmail.com, xiyou...@googlegroups.com
From: vvoody <wxj....@gmail.com>
Date: Fri, 18 Apr 2008 19:51:57 +0800
Yes. Excellent!! ;-)

刘洋

unread,
Apr 18, 2008, 7:24:56 PM4/18/08
to Jianjun Kong, 李磊, 西邮Linux兴趣小组

这个说法正确吗?你的编译器是什么?标准规定的是long等于机器字长,short小于long,而int只要不小于short也不大于long就可以了。而且事实上,GCC和现在VC++在我的电脑上,int都是32bit的!

ChopDown

unread,
Apr 26, 2008, 1:52:58 AM4/26/08
to 西邮Linux兴趣小组
留个脚印. 楼内的解决方法效率太低: 除法跟取余操作都费时, 循环内使用printf一个一个打印出来更没效率. 贴一个自己写的
理论上, 兼容64位

VOID Printf_Binary(int buf)
{
UINT32 size = sizeof(int)*8;
CHAR tmp[sizeof(int)*8+1];
CHAR cnt;

tmp[size] = '\0';

for (cnt=0; cnt<size; cnt++)
{
tmp[size-cnt-1] = ((buf >> cnt) & 0x1)+'0';
}
printf("%s", tmp);
}

On Apr 17, 7:43 pm, Jianjun Kong <kongjian...@gmail.com> wrote:
> 关于C/C++打印二进制这里有很多方法http://www.52unix.net/software/p613/A61375445.shtml

WANG Cong

unread,
Apr 26, 2008, 3:32:19 AM4/26/08
to Chop...@gmail.com, xiyou...@googlegroups.com
From: ChopDown <Chop...@gmail.com>
Date: Fri, 25 Apr 2008 22:52:58 -0700 (PDT)
> 留个脚印. 楼内的解决方法效率太低: 除法跟取余操作都费时, 循环内使用printf一个一个打印出来更没效率. 贴一个自己写的
> 理论上, 兼容64位
>
> VOID Printf_Binary(int buf)

C是大小写敏感的。VOID不等于void。;-)


> {
> UINT32 size = sizeof(int)*8;

目前绝大多数实现中int都是4个字节,不论32bit还是64bit。

另外,标准的32bit无符号整数类型是uint32_t,在<stdint.h>里。 ;-)

> CHAR tmp[sizeof(int)*8+1];

如果非得挑刺的话,你应该用CHAR_BIT而不是用8。 8-)


> CHAR cnt;
>

用int,一般来说。

> tmp[size] = '\0';
>
> for (cnt=0; cnt<size; cnt++)
> {
> tmp[size-cnt-1] = ((buf >> cnt) & 0x1)+'0';
> }
> printf("%s", tmp);


换行,因为这是stdout。


> }
>

Niu Tao

unread,
Apr 26, 2008, 4:06:27 AM4/26/08
to Jianjun Kong, 西邮Linux兴趣小组
2008/4/17 Jianjun Kong <kongj...@gmail.com>:

> 关于C/C++打印二进制这里有很多方法
> http://www.52unix.net/software/p613/A61375445.shtml
> 我试了一下,有些还有问题,有没有比较正规的解决方法。
>
我在测试第三个的时候,突然发现了一个问题:
将其简化写成这样:
#include<stdio.h>
int main()
{
float f;
printf("please input a float number:");
scanf("%f",&f);
printf("f=%f\n",f);
return 0;
}
运行:
please input a float number:123.3
f=123.300003
Press any key to continue
为什么?应该是:
f=123.300000才是呀?

ChopDown

unread,
Apr 26, 2008, 10:30:40 AM4/26/08
to 西邮Linux兴趣小组
抱歉, 为了解决兼容性的问题(不同的处理器的类型长度等), 我公司都是规定用type定义各种类型的. 一时写习惯了, 就收不了手了.
至于int, 其实应该写成SINT32,表示符号32位, 不过写的时候想了楼内兼容64的问题, 就直接写int了.
应该存在64bit的情况, 大概可以用参数调出来 :D

On Apr 26, 3:32 pm, WANG Cong <xiyou.wangc...@gmail.com> wrote:
> From: ChopDown <ChopD...@gmail.com>

ChopDown

unread,
Apr 26, 2008, 10:35:23 AM4/26/08
to 西邮Linux兴趣小组
浮点型是有误差的....特别是处理那种除不尽的情况.

On Apr 26, 4:06 pm, "Niu Tao" <niutao0...@gmail.com> wrote:
> 2008/4/17 Jianjun Kong <kongjian...@gmail.com>:> 关于C/C++打印二进制这里有很多方法

Jianjun Kong

unread,
Apr 26, 2008, 6:35:16 PM4/26/08
to ChopDown, 西邮Linux兴趣小组
On Fri, Apr 25, 2008 at 10:52:58PM -0700, ChopDown wrote:
> 留个脚印. 楼内的解决方法效率太低: 除法跟取余操作都费时, 循环内使用printf一个一个打印出来更没效率. 贴一个自己写的
> 理论上, 兼容64位
>
> VOID Printf_Binary(int buf)
> {
> UINT32 size = sizeof(int)*8;
> CHAR tmp[sizeof(int)*8+1];
> CHAR cnt;
>
> tmp[size] = '\0';
>
> for (cnt=0; cnt<size; cnt++)
> {
> tmp[size-cnt-1] = ((buf >> cnt) & 0x1)+'0';
> }
> printf("%s", tmp);
> }
你的程序输出了前面多余的0

#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!
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Niu Tao

unread,
Apr 26, 2008, 10:40:37 PM4/26/08
to Jianjun Kong, ChopDown, 西邮Linux兴趣小组
2008/4/27 Jianjun Kong <kongj...@gmail.com>:
这样也可以:
#include <stdio.h>
#include <stdint.h>
#include<string.h>

void Printf_Binary(int buf)
{
uint32_t size = sizeof(int)*8;
char tmp[sizeof(int)*8+1];
char 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);
}

int main()
{
int i;
printf("Input a integer number:");
scanf("%d",&i);
Printf_Binary(i);
return 0;
}

Niu Tao

unread,
Apr 26, 2008, 10:42:22 PM4/26/08
to Jianjun Kong, ChopDown, 西邮Linux兴趣小组
2008/4/27 Niu Tao <niuta...@gmail.com>:

上一个不小心,没发好:

WANG Cong

unread,
Apr 26, 2008, 10:47:23 PM4/26/08
to niuta...@gmail.com, kongj...@gmail.com, Chop...@gmail.com, xiyou...@googlegroups.com
From: "Niu Tao" <niuta...@gmail.com>
Date: Sun, 27 Apr 2008 10:42:22 +0800

<snip>

> #include <stdio.h>
> #include <stdint.h>
> #include<string.h>
> void Printf_Binary(int buf)
> {
> uint32_t size = sizeof(int)*8;
> char tmp[sizeof(int)*8+1];
> char 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);
> }
>
> int main()
> {
> int i;
> printf("Input a integer number:");
> scanf("%d",&i);
> Printf_Binary(i);
> return 0;
> }

foo.c:5: 警告:‘Printf_Binary’ 先前没有原型
foo.c: 在函数 ‘Printf_Binary’ 中:
foo.c:13: 警告:比较有符号和无符号数
foo.c: 在顶层:
foo.c:23: 警告:函数声明不是一个原型


Input a integer number:0
2676 段错误

Niu Tao

unread,
Apr 26, 2008, 10:56:55 PM4/26/08
to WANG Cong, kongj...@gmail.com, Chop...@gmail.com, xiyou...@googlegroups.com


2008/4/27 WANG Cong <xiyou.w...@gmail.com>:

改了一下:

#include <stdio.h>
#include <stdint.h>
#include<string.h>
void Printf_Binary(int buf)
{
        uint32_t 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("Input a integer number:");
        scanf("%d",&i);
        Printf_Binary(i);
        return 0;
}
niutao@niutao-desktop:~/c$ gcc -Wall -o a a.c
niutao@niutao-desktop:~/c$ ./a

Input a integer number:0

niutao@niutao-desktop:~/c$ ./a
Input a integer number:123
1111011
niutao@niutao-desktop:~/c$ 

Niu Tao

unread,
Apr 26, 2008, 11:11:13 PM4/26/08
to Jianjun Kong, ChopDown, 西邮Linux兴趣小组
2008/4/27 Jianjun Kong <kongj...@gmail.com>:
你这里的"%i"和通常用的"%d"有什么区别?
> Printf_Binary(i);
> return 0;
> }

WANG Cong

unread,
Apr 26, 2008, 11:22:36 PM4/26/08
to niuta...@gmail.com, kongj...@gmail.com, Chop...@gmail.com, xiyou...@googlegroups.com
From: "Niu Tao" <niuta...@gmail.com>
Date: Sun, 27 Apr 2008 11:11:13 +0800

<snip>

> > int main()
> > {
> > int i;
> > scanf("%i",&i);
> 你这里的"%i"和通常用的"%d"有什么区别?

% man scanf

[...]

i Matches an optionally signed integer; the next pointer must be a
pointer to int. The integer is read in base 16 if it begins
with 0x or 0X, in base 8 if it begins with 0, and in base 10
otherwise. Only characters that correspond to the base are
used.

ChopDown

unread,
Apr 27, 2008, 12:45:30 AM4/27/08
to 西邮Linux兴趣小组
这段改的不错, 学习了. :)

On Apr 27, 10:56 am, "Niu Tao" <niutao0...@gmail.com> wrote:
> 2008/4/27 WANG Cong <xiyou.wangc...@gmail.com>:

赵尔凡

unread,
Apr 27, 2008, 12:24:01 PM4/27/08
to 西邮Linux兴趣小组, chop...@gmail.com
我觉得出于程序的健壮性考虑,我觉得应该先判断下输入的字符。如果为数字,一切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);

printf("Input a integer number:");
scanf("%d",&i);
+ if( isdigit(i))
+ Printf_Binary_digit(i);
+ else
+ { printf("wrong input,please check it again ^|^\n");
+ return 1;
+ }
return 0;
}

> > niutao@niutao-desktop:~/c$ ./a
> > Input a integer number:123
> > 1111011
> > niutao@niutao-desktop:~/c$- 隐藏被引用文字 -


> - 显示引用的文字 -

WANG Cong

unread,
Apr 27, 2008, 11:17:43 PM4/27/08
to 赵尔凡, 西邮Linux兴趣小组, chop...@gmail.com
On Sun, 27 Apr 2008, 赵尔凡 wrote:

<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.

Reply all
Reply to author
Forward
0 new messages