Lebih
Apakah ada konverter printf untuk mencetak dalam format biner?
Saya dapat mencetak dengan printf sebagai bilangan heksa atau oktal. Apakah ada tag format untuk mencetak sebagai biner, atau basis sembarang?
Saya menjalankan gcc.
printf("%d %x %o\n", 10, 10, 10); //prints "10 A 12\n"
print("%b\n", 10); // prints "%b\n"
400
20
Hacky tapi bekerja untuk saya.
Untuk tipe multi-byte
Sayangnya kau butuh semua kutipan tambahan. Pendekatan ini memiliki risiko efisiensi dari makro (jangan lulus sebuah fungsi sebagai argumen ke `BYTE_Tapi menghindari masalah memori dan beberapa undangan strcat dalam beberapa proposal lain di sini.
Cetak Binari untuk Setiap Datatype
uji
Berikut adalah hack cepat untuk menunjukkan teknik untuk melakukan apa yang Anda inginkan.
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.
You could use a small table to improve speed1. Similar techniques are useful in the embedded world, for example, to invert a byte:
1 I'm mostly referring to embedded applications where optimizers are not so aggressive and the speed difference is visible.
Print the least significant bit and shift it out on the right. Doing this until the integer becomes zero prints the binary representation without leading zeros but in reversed order. Using recursion, the order can be corrected quite easily.
To me, this is one of the cleanest solutions to the problem. If you like
0b
prefix and a trailing new line character, I suggest wrapping the function.Online demo
Based on @William Whyte's answer, this is a macro that provides
int8
,16
,32
&64
versions, reusing theINT8
macro to avoid repetition.This outputs:
For readability you may want to add a separator for eg:
Here's a version of the function that does not suffer from reentrancy issues or limits on the size/type of the argument:
Note that this code would work just as well for any base between 2 and 10 if you just replace the 2's by the desired base. Usage is:
Where
x
is any integral expression.None of the previously posted answers are exactly what I was looking for, so I wrote one. It is super simple to use %B with the
printf
!Beberapa runtime mendukung "%b" meskipun itu bukan standar.
Juga lihat di sini untuk diskusi yang menarik:
http://bytes.com/forum/thread591027.html
HTH
The
printf()
family is only able to print in base 8, 10, and 16 using the standard specifiers directly. I suggest creating a function that converts the number to a string per code's particular needs.To print in any base [2-36]
All other answers so far have at least one of these limitations.
Use static memory for the return buffer. This limits the number of times the function may be used as an argument to
printf()
.Allocate memory requiring the calling code to free pointers.
Require the calling code to explicitly provide a suitable buffer.
Call
printf()
directly. This obliges a new function for tofprintf()
,sprintf()
,vsprintf()
, etc.Use a reduced integer range.
The following has none of the above limitation. It does require C99 or later and use of
"%s"
. It uses a compound literal to provide the buffer space. It has no trouble with multiple calls in aprintf()
.This code should handle your needs up to 64 bits. I created 2 functions pBin & pBinFill. Both do the same thing, but pBinFill fills in the leading spaces with the fillChar. The test function generates some test data, then prints it out using the function.
Maybe a bit OT, but if you need this only for debuging to understand or retrace some binary operations you are doing, you might take a look on wcalc (a simple console calculator). With the -b options you get binary output.
e.g.
Tidak ada fungsi pemformatan dalam pustaka standar C untuk mengeluarkan biner seperti itu. Semua operasi format yang didukung keluarga printf adalah untuk teks yang dapat dibaca manusia.
The following recursive function might be useful:
I optimized the top solution for size and C++-ness, and got to this solution:
Tidak ada cara standar dan portabel.
Beberapa implementasi menyediakan itoa(), tetapi tidak akan ada di sebagian besar, dan memiliki antarmuka yang agak payah. Tetapi kodenya ada di belakang tautan dan seharusnya memungkinkan Anda mengimplementasikan formatter Anda sendiri dengan cukup mudah.
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.