问题描述
嗨!
如何使用printf()以二进制形式输出char或int的值;提前
thanx
二进制表格没有标准格式说明符。您将手动进行转换
,从最高位到最低位b / b测试每一位,如果未设置则打印0,以及'' 1''如果是的话。
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
HTH,
-Beej
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.
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
http://c-faq.com/misc/base2.html
http://c-faq.com/misc/hexio.html
HTH,
-Beej
这篇关于打印二进制表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!