Is there a printf converter to print in binary format?

前端 未结 30 2665
盖世英雄少女心
盖世英雄少女心 2020-11-21 06:20

I can print with printf as a hex or octal number. Is there a format tag to print as binary, or arbitrary base?

I am running gcc.

printf(\"%d %x %o         


        
相关标签:
30条回答
  • 2020-11-21 06:35
    const char* byte_to_binary(int x)
    {
        static char b[sizeof(int)*8+1] = {0};
        int y;
        long long z;
    
        for (z = 1LL<<sizeof(int)*8-1, y = 0; z > 0; z >>= 1, y++) {
            b[y] = (((x & z) == z) ? '1' : '0');
        }
        b[y] = 0;
    
        return b;
    }
    
    0 讨论(0)
  • 2020-11-21 06:38

    I liked the code by paniq, the static buffer is a good idea. However it fails if you want multiple binary formats in a single printf() because it always returns the same pointer and overwrites the array.

    Here's a C style drop-in that rotates pointer on a split buffer.

    char *
    format_binary(unsigned int x)
    {
        #define MAXLEN 8 // width of output format
        #define MAXCNT 4 // count per printf statement
        static char fmtbuf[(MAXLEN+1)*MAXCNT];
        static int count = 0;
        char *b;
        count = count % MAXCNT + 1;
        b = &fmtbuf[(MAXLEN+1)*count];
        b[MAXLEN] = '\0';
        for (int z = 0; z < MAXLEN; z++) { b[MAXLEN-1-z] = ((x>>z) & 0x1) ? '1' : '0'; }
        return b;
    }
    
    0 讨论(0)
  • 2020-11-21 06:40

    Based on @William Whyte's answer, this is a macro that provides int8,16,32 & 64 versions, reusing the INT8 macro to avoid repetition.

    /* --- PRINTF_BYTE_TO_BINARY macro's --- */
    #define PRINTF_BINARY_PATTERN_INT8 "%c%c%c%c%c%c%c%c"
    #define PRINTF_BYTE_TO_BINARY_INT8(i)    \
        (((i) & 0x80ll) ? '1' : '0'), \
        (((i) & 0x40ll) ? '1' : '0'), \
        (((i) & 0x20ll) ? '1' : '0'), \
        (((i) & 0x10ll) ? '1' : '0'), \
        (((i) & 0x08ll) ? '1' : '0'), \
        (((i) & 0x04ll) ? '1' : '0'), \
        (((i) & 0x02ll) ? '1' : '0'), \
        (((i) & 0x01ll) ? '1' : '0')
    
    #define PRINTF_BINARY_PATTERN_INT16 \
        PRINTF_BINARY_PATTERN_INT8              PRINTF_BINARY_PATTERN_INT8
    #define PRINTF_BYTE_TO_BINARY_INT16(i) \
        PRINTF_BYTE_TO_BINARY_INT8((i) >> 8),   PRINTF_BYTE_TO_BINARY_INT8(i)
    #define PRINTF_BINARY_PATTERN_INT32 \
        PRINTF_BINARY_PATTERN_INT16             PRINTF_BINARY_PATTERN_INT16
    #define PRINTF_BYTE_TO_BINARY_INT32(i) \
        PRINTF_BYTE_TO_BINARY_INT16((i) >> 16), PRINTF_BYTE_TO_BINARY_INT16(i)
    #define PRINTF_BINARY_PATTERN_INT64    \
        PRINTF_BINARY_PATTERN_INT32             PRINTF_BINARY_PATTERN_INT32
    #define PRINTF_BYTE_TO_BINARY_INT64(i) \
        PRINTF_BYTE_TO_BINARY_INT32((i) >> 32), PRINTF_BYTE_TO_BINARY_INT32(i)
    /* --- end macros --- */
    
    #include <stdio.h>
    int main() {
        long long int flag = 1648646756487983144ll;
        printf("My Flag "
               PRINTF_BINARY_PATTERN_INT64 "\n",
               PRINTF_BYTE_TO_BINARY_INT64(flag));
        return 0;
    }
    

    This outputs:

    My Flag 0001011011100001001010110111110101111000100100001111000000101000
    

    For readability you may want to add a separator for eg:

    My Flag 00010110,11100001,00101011,01111101,01111000,10010000,11110000,00101000
    
    0 讨论(0)
  • 2020-11-21 06:46

    There is no formatting function in the C standard library to output binary like that. All the format operations the printf family supports are towards human readable text.

    0 讨论(0)
  • 2020-11-21 06:46

    Use:

    char buffer [33];
    itoa(value, buffer, 2);
    printf("\nbinary: %s\n", buffer);
    

    For more ref., see How to print binary number via printf.

    0 讨论(0)
  • 2020-11-21 06:47

    There isn't a binary conversion specifier in glibc normally.

    It is possible to add custom conversion types to the printf() family of functions in glibc. See register_printf_function for details. You could add a custom %b conversion for your own use, if it simplifies the application code to have it available.

    Here is an example of how to implement a custom printf formats in glibc.

    0 讨论(0)
提交回复
热议问题