如何将int转换为二进制形式? [英] How do I convert an int to binary form?

查看:144
本文介绍了如何将int转换为二进制形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写一个简单的代码时遇到了麻烦,该代码只是为了打印

二进制文件而将int(例如
487)转换为二进制形式。有人可以帮忙吗?谢谢!

I am having trouble writing a simple code that will convert an int (
487 for example ) to binary form just for the sake of printing the
binary. Can someone please help? Thanks!

推荐答案

< ce ****** @ gmail.com>写道:
<ce******@gmail.com> wrote:
我在编写一个简单的代码时遇到问题,该代码将int(例如,例如487)转换为二进制形式,仅仅是为了打印
二进制文件。有人可以帮忙吗?谢谢!
I am having trouble writing a simple code that will convert an int (
487 for example ) to binary form just for the sake of printing the
binary. Can someone please help? Thanks!




使用左移操作符和&运营商。当你第一次在最左边的位置遇到1时,跳过前导零和

,打印一个字符1。

继续移动并打印1或0取决于int。



Use the left shift operator and the & operator. Skip the leading zeros and
when you first encounter a 1 in the leftmost position, print a char ''1''.
Keep shifting and printing ''1'' or ''0'' depending on the int.


ce *** ***@gmail.com 写道:
我在编写一个简单的代码时遇到了麻烦,该代码将int(例如
487)转换为二进制形式打印
二进制文件。有人可以帮忙吗?谢谢!
I am having trouble writing a simple code that will convert an int (
487 for example ) to binary form just for the sake of printing the
binary. Can someone please help? Thanks!




请参阅comp.lang.c常见问题解答中的问题20.10,< http://www.c-faq.com/>。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



See question 20.10 in the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


ce ****** @ gmail.com 写道:

我正在编写一个简单的代码,将一个int(例如
487)转换为二进制形式只是为了这个缘故打印
二进制文件。有人可以帮忙吗?谢谢!

I am having trouble writing a simple code that will convert an int (
487 for example ) to binary form just for the sake of printing the
binary. Can someone please help? Thanks!




这一次,如果你读了我的签名和那里的U​​RL

引用。


#include< stdio.h>


/ * ---------------------- * /


static void putbin(unsigned int i,FILE * f){


if(i / 2)putbin(i / 2,f) ;

putc((i& 1)+''0'',f);

} / * putbin * /


/ * ---------------------- * /


int main(void){


putbin(0,stdout); putc(''\ n'',stdout);

putbin(1,stdout); putc(''\ n'',stdout);

putbin(-1,stdout); putc(''\ n'',stdout);

putbin(2,stdout); putc(''\ n'',stdout);

putbin(23,stdout); putc(''\ n'',stdout);

putbin(27,stdout); putc(''\ n'',stdout);

返回0;

} / * main * /


-

"如果你想通过groups.google.com发布一个后续内容,请不要使用

破坏的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson

更多详细信息:< http://cfaj.freeshell.org/google/>

另见< http://www.safalra .com / special / googlegroupsreply />



Just this once, provided you read my sig and the URLs there
referenced.

#include <stdio.h>

/* ---------------------- */

static void putbin(unsigned int i, FILE *f) {

if (i / 2) putbin(i / 2, f);
putc((i & 1) + ''0'', f);
} /* putbin */

/* ---------------------- */

int main(void) {

putbin( 0, stdout); putc(''\n'', stdout);
putbin( 1, stdout); putc(''\n'', stdout);
putbin(-1, stdout); putc(''\n'', stdout);
putbin( 2, stdout); putc(''\n'', stdout);
putbin(23, stdout); putc(''\n'', stdout);
putbin(27, stdout); putc(''\n'', stdout);
return 0;
} /* main */

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


这篇关于如何将int转换为二进制形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆