在C中显示数字的二进制表示?

Pau*_*cks 6 c binary printf representation

可能重复:
是否有printf转换器以二进制格式打印?

还在学习C和我在想:

给定一个数字,是否可以执行以下操作?

char a = 5;
printf("binary representation of a = %b",a);
> 101
Run Code Online (Sandbox Code Playgroud)

或者我是否必须编写自己的方法来转换为二进制?

dir*_*tly 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));
}
Run Code Online (Sandbox Code Playgroud)

如果您正在使用终端,则可以使用控制代码按自然顺序打印字节:

void printbits(unsigned char v) {
    printf("%*s", (int)ceil(log2(v)) + 1, ""); 
    for (; v; v >>= 1) printf("\x1b[2D%c",'0' + (v & 1));
}
Run Code Online (Sandbox Code Playgroud)

  • 明显的错误 - 它以相反的顺序打印位.非明显的错误 - 它不会为v = 0打印任何内容. (7认同)
  • 好一点黑客.不可读的代码FTW! (2认同)

Chr*_*utz 24

根据dirkgently的答案,但修复他的两个错误,并始终打印固定数字的数字:

void printbits(unsigned char v) {
  int i; // for C89 compatability
  for(i = 7; i >= 0; i--) putchar('0' + ((v >> i) & 1));
}
Run Code Online (Sandbox Code Playgroud)

  • 作为锦上添花,您还可以用(sizeof(v)*8)-1替换i = 7 (2认同)

pax*_*blo 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;
}
Run Code Online (Sandbox Code Playgroud)

将此main添加到它的末尾以查看它的运行情况:

#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;
}
Run Code Online (Sandbox Code Playgroud)

运行它"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')
Run Code Online (Sandbox Code Playgroud)


nir*_*pam 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;
}
Run Code Online (Sandbox Code Playgroud)