打印二进制表示 [英] print binary representation

查看:148
本文介绍了打印二进制表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


如何使用printf()以二进制形式输出char或int的值;提前
thanx

解决方案

Carramba写道:


嗨!


如何使用printf()以二进制形式输出char或int的值; ?


提前预付



二进制表格没有标准格式说明符。您将手动进行转换

,从最高位到最低位b / b测试每一位,如果未设置则打印0,以及'' 1''如果是的话。


2007年3月24日星期六08:55:36 +0100,Carramba< us ** @ example.netwrote :


>嗨!

如何用printf()以二进制形式输出char或int的值; ?

thanx提前



C标准没有为printf()定义转换说明符

二进制输出。唯一可行的方法是滚动你的

。这是一个开始:


printf("%s \ n",int_to_binary_string(my_int));


确保何时你实现int_to_binary_string(),它与

大多数桌面目标一起使用sizeof(int)* CHAR_BIT = 32以及许多

嵌入式目标,其中sizeof(int)* CHAR_BIT = 16.


最好的问候

-

jay


Carramba< us ** @ example.netwrote:


>如何使用printf()以二进制形式输出char或int的值; ?

http://c-faq.com/ misc / base2.html

http ://c-faq.com/misc/hexio.html


HTH,

-Beej


Hi!

How can I output value of char or int in binary form with printf(); ?

thanx in advance

解决方案

Carramba wrote:

Hi!

How can I output value of char or int in binary form with printf(); ?

thanx in advance

There is no standard format specifier for binary form. You will have
to do the conversion manually, testing each bit from highest to
lowest, printing ''0'' if it''s not set, and ''1'' if it is.


On Sat, 24 Mar 2007 08:55:36 +0100, Carramba <us**@example.netwrote:

>Hi!

How can I output value of char or int in binary form with printf(); ?

thanx in advance

The C Standards do not define a conversion specifier for printf() to
output in binary. The only portable way to do this is to roll your
own. Here''s a start:

printf("%s\n", int_to_binary_string(my_int));

Make sure when you implement int_to_binary_string() that it works with
most desktop targets where sizeof(int) * CHAR_BIT = 32 as well on many
embedded targets where sizeof(int) * CHAR_BIT = 16.

Best regards
--
jay


Carramba <us**@example.netwrote:

>How can I output value of char or int in binary form with printf(); ?

http://c-faq.com/misc/base2.html

http://c-faq.com/misc/hexio.html

HTH,
-Beej


这篇关于打印二进制表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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