在C中显示数字的二进制表示?
可能的重复:
是否有 printf 转换器可以以二进制格式打印?< /a>
仍在学习 C,我想知道:
给定一个数字,是否可以执行如下操作?
char a = 5;
printf("binary representation of a = %b",a);
> 101
或者我是否必须编写自己的方法来转换为二进制?
Possible Duplicate:
Is there a printf converter to print in binary format?
Still learning C and I was wondering:
Given a number, is it possible to do something like the following?
char a = 5;
printf("binary representation of a = %b",a);
> 101
Or would i have to write my own method to do the transformation to binary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
没有直接的方法(即使用 printf 或其他标准库函数)来打印它。 您必须编写自己的函数。
如果您使用终端,则可以使用控制代码以自然顺序打印字节:
There is no direct way (i.e. using
printf
or another standard library function) to print it. You will have to write your own function.If you're using terminal, you can use control codes to print out bytes in natural order:
基于 dirkgently 的回答,但是修复他的两个错误,并始终打印固定数量的数字:
Based on dirkgently's answer, but fixing his two bugs, and always printing a fixed number of digits:
是的(你自己写),类似于下面的完整函数。
将此 main 添加到其末尾以查看其运行情况:
使用
"progname 0 7 12 52 123"
运行它以获取:Yes (write your own), something like the following complete function.
Add this main to the end of it to see it in operation:
Run it with
"progname 0 7 12 52 123"
to get:使用查找表,例如:
然后像这样打印每个半字节
当然您可以只使用一个表,但它会稍微快一点并且太大。
Use a lookup table, like:
then print each nibble like this
Surely you can use just one table, but it will be marginally faster and too big.
C 语言中没有直接的格式说明符。 尽管我编写了这个快速的 python 代码片段来帮助您逐步了解自己的流程。
解释:
dec = input("输入要转换的十进制数:") - 提示用户输入数字(在 C 中通过 scanf 有多种方法可以做到这一点)示例)
base = 2 - 指定我们的基数为 2(二进制)
solution = "" - 创建一个空字符串,我们将在其中连接我们的解决方案
while dec > ;= base: - 当我们的数字大于输入的基数时
solution = str(dec%base) + Solution - 获取数字与基数的模,并将其添加到字符串的开头(我们必须使用除法和余数方法从右到左添加数字)。 str() 函数将运算结果转换为字符串。 在 python 中,如果不进行类型转换,就无法将整数与字符串连接起来。
dec = dec/base - 将十进制数除以基数以准备取下一个模
if dec > 0:
solution = str(dec) + Solution - 如果还有剩余内容,请将其添加到开头(如果有的话,这将是 1)
打印解决方案 - 打印最终数字
There is no direct format specifier for this in the C language. Although I wrote this quick python snippet to help you understand the process step by step to roll your own.
Explained:
dec = input("Enter a decimal number to convert: ") - prompt the user for numerical input (there are multiple ways to do this in C via scanf for example)
base = 2 - specify our base is 2 (binary)
solution = "" - create an empty string in which we will concatenate our solution
while dec >= base: - while our number is bigger than the base entered
solution = str(dec%base) + solution - get the modulus of the number to the base, and add it to the beginning of our string (we must add numbers right to left using division and remainder method). the str() function converts the result of the operation to a string. You cannot concatenate integers with strings in python without a type conversion.
dec = dec/base - divide the decimal number by the base in preperation to take the next modulo
if dec > 0:
solution = str(dec) + solution - if anything is left over, add it to the beginning (this will be 1, if anything)
print solution - print the final number
此代码最多可满足您的 64 位需求。
This code should handle your needs up to 64 bits.
您必须编写自己的转换。 格式说明符仅支持十进制、十六进制和八进制数字。
You have to write your own transformation. Only decimal, hex and octal numbers are supported with format specifiers.