可能重复:
Is there a printf converter to print in binary format?
还在学习C和我在想:
给定一个数字,是否可以执行以下操作?
char a = 5;
printf("binary representation of a = %b",a);
> 101
或者我是否必须编写自己的方法来转换为二进制文件?
答案 0 :(得分:28)
没有直接的方式(即使用printf
或其他标准库函数)来打印它。你必须编写自己的函数。
/* This code has an obvious bug and another non-obvious one :) */
void printbits(unsigned char v) {
for (; v; v >>= 1) putchar('0' + (v & 1));
}
如果您正在使用终端,则可以使用控制代码按自然顺序打印字节:
void printbits(unsigned char v) {
printf("%*s", (int)ceil(log2(v)) + 1, "");
for (; v; v >>= 1) printf("\x1b[2D%c",'0' + (v & 1));
}
答案 1 :(得分:24)
基于dirkgently's answer,但修复了他的两个错误,并始终打印固定数量的数字:
void printbits(unsigned char v) {
int i; // for C89 compatability
for(i = 7; i >= 0; i--) putchar('0' + ((v >> i) & 1));
}
答案 2 :(得分:12)
是(自己编写),类似于以下完整功能。
#include <stdio.h> /* only needed for the printf() in main(). */
#include <string.h>
/* Create a string of binary digits based on the input value.
Input:
val: value to convert.
buff: buffer to write to must be >= sz+1 chars.
sz: size of buffer.
Returns address of string or NULL if not enough space provided.
*/
static char *binrep (unsigned int val, char *buff, int sz) {
char *pbuff = buff;
/* Must be able to store one character at least. */
if (sz < 1) return NULL;
/* Special case for zero to ensure some output. */
if (val == 0) {
*pbuff++ = '0';
*pbuff = '\0';
return buff;
}
/* Work from the end of the buffer back. */
pbuff += sz;
*pbuff-- = '\0';
/* For each bit (going backwards) store character. */
while (val != 0) {
if (sz-- == 0) return NULL;
*pbuff-- = ((val & 1) == 1) ? '1' : '0';
/* Get next bit. */
val >>= 1;
}
return pbuff+1;
}
将此主要内容添加到其末尾以便在操作中查看它:
#define SZ 32
int main(int argc, char *argv[]) {
int i;
int n;
char buff[SZ+1];
/* Process all arguments, outputting their binary. */
for (i = 1; i < argc; i++) {
n = atoi (argv[i]);
printf("[%3d] %9d -> %s (from '%s')\n", i, n,
binrep(n,buff,SZ), argv[i]);
}
return 0;
}
使用"progname 0 7 12 52 123"
运行以获取:
[ 1] 0 -> 0 (from '0')
[ 2] 7 -> 111 (from '7')
[ 3] 12 -> 1100 (from '12')
[ 4] 52 -> 110100 (from '52')
[ 5] 123 -> 1111011 (from '123')
答案 3 :(得分:6)
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void displayBinary(int n)
{
char bistr[1000];
itoa(n,bistr,2); //2 means binary u can convert n upto base 36
printf("%s",bistr);
}
int main()
{
int n;
cin>>n;
displayBinary(n);
getch();
return 0;
}
答案 4 :(得分:4)
使用查找表,例如:
char *table[16] = {"0000", "0001", .... "1111"};
然后像这样打印每个半字节
printf("%s%s", table[a / 0x10], table[a % 0x10]);
当然,你只能使用一张桌子,但它会稍快一点,而且太大了。
答案 5 :(得分:3)
此代码最多可满足您64位的需求。
char* pBinFill(long int x,char *so, char fillChar); // version with fill
char* pBin(long int x, char *so); // version without fill
#define width 64
char* pBin(long int x,char *so)
{
char s[width+1];
int i=width;
s[i--]=0x00; // terminate string
do
{ // fill in array from right to left
s[i--]=(x & 1) ? '1':'0'; // determine bit
x>>=1; // shift right 1 bit
} while( x > 0);
i++; // point to last valid character
sprintf(so,"%s",s+i); // stick it in the temp string string
return so;
}
char* pBinFill(long int x,char *so, char fillChar)
{ // fill in array from right to left
char s[width+1];
int i=width;
s[i--]=0x00; // terminate string
do
{
s[i--]=(x & 1) ? '1':'0';
x>>=1; // shift right 1 bit
} while( x > 0);
while(i>=0) s[i--]=fillChar; // fill with fillChar
sprintf(so,"%s",s);
return so;
}
void test()
{
char so[width+1]; // working buffer for pBin
long int val=1;
do
{
printf("%ld =\t\t%#lx =\t\t0b%s\n",val,val,pBinFill(val,so,0));
val*=11; // generate test data
} while (val < 100000000);
}
Output:
00000001 = 0x000001 = 0b00000000000000000000000000000001
00000011 = 0x00000b = 0b00000000000000000000000000001011
00000121 = 0x000079 = 0b00000000000000000000000001111001
00001331 = 0x000533 = 0b00000000000000000000010100110011
00014641 = 0x003931 = 0b00000000000000000011100100110001
00161051 = 0x02751b = 0b00000000000000100111010100011011
01771561 = 0x1b0829 = 0b00000000000110110000100000101001
19487171 = 0x12959c3 = 0b00000001001010010101100111000011
答案 6 :(得分:2)
你必须编写自己的转型。格式说明符仅支持十进制,十六进制和八进制数。
答案 7 :(得分:2)
C语言中没有直接的格式说明符。虽然我编写了这个快速的python片段,以帮助您逐步了解该过程,以推动自己的过程。
#!/usr/bin/python
dec = input("Enter a decimal number to convert: ")
base = 2
solution = ""
while dec >= base:
solution = str(dec%base) + solution
dec = dec/base
if dec > 0:
solution = str(dec) + solution
print solution
<强>解释强>
dec = input(“输入要转换的十进制数字:”) - 提示用户输入数字(例如,有多种方法可以通过scanf在C中执行此操作)
base = 2 - 指定我们的基数为2(二进制)
solution =“” - 创建一个空字符串,我们将在其中连接我们的解决方案
而dec&gt; = base: - 我们的号码大于输入的基数
solution = str(dec%base)+ solution - 获取数字的模数到基数,并将其添加到字符串的开头(我们必须使用除法从右到左添加数字和剩余方法)。 str()函数将操作的结果转换为字符串。没有类型转换,你不能在python中将整数与字符串连接起来。
dec = dec / base - 在准备中将十进制数除以基数以取下一个模数
如果dec&gt; 0:强> solution = str(dec)+ solution - 如果遗留任何内容,请将其添加到开头(如果有的话,这将是1)
打印解决方案 - 打印最终号码