Is there a printf converter to print in binary format?

前端 未结 30 2742
盖世英雄少女心
盖世英雄少女心 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: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.

提交回复
热议问题