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\n", 10, 10, 10); //prints "10 A 12\n"
print("%b\n", 10); // prints "%b\n"
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\n", 10, 10, 10); //prints "10 A 12\n"
print("%b\n", 10); // prints "%b\n"
You can not do this, as far as I know, using printf.
You could, obviously, write a helper method to accomplish this, but that doesn't sound like the direction you're wanting to go.
There is no formating function in the C standard library to output binary like that. All the format operation the printf family supports are towards human readable text.
There isn't a format predefined for that. You need to transform it yourself to a string and then print the string.
A quick Google search produced this page with some information that may be useful:
Some runtimes support "%b" although that is not a standard.
Also see here for an interesting discussion:
http://bytes.com/forum/thread591027.html
HTH
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.
Here is a quick hack to demonstrate techniques to do what you want.
#include <stdio.h> // printf
#include <string.h> // strcat
#include <stdlib.h> // strtol
const char *byte_to_binary
(
int x
)
{
static char b[9];
b[0] = '\0';
int z;
for (z = 256; z > 0; z >>= 1)
{
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
int main(void)
{
{
// binary string to int
char *tmp;
char *b = "0101";
printf("%d\n", strtol(b, &tmp, 2));
}
{
// byte to binary string
printf("%s\n", byte_to_binary(5));
}
return 0;
}
const char* byte_to_binary( int x )
{
static char b[8] = {0};
for (int z=128,y=0; z>0; z>>=1,y++)
{
b[y] = ( ((x & z) == z) ? 49 : 48);
}
return b;
}
Even for the runtime libraries that DO support %b it seems it's only for integer values.
If you want to print floating-point values in binary, I wrote some code you can find at http://www.exploringbinary.com/converting-floating-point-numbers-to-binary-strings-in-c/ .
Hacky but works for me:
#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(byte) \
(byte & 0x80 ? 1 : 0), \
(byte & 0x40 ? 1 : 0), \
(byte & 0x20 ? 1 : 0), \
(byte & 0x10 ? 1 : 0), \
(byte & 0x08 ? 1 : 0), \
(byte & 0x04 ? 1 : 0), \
(byte & 0x02 ? 1 : 0), \
(byte & 0x01 ? 1 : 0)
...
printf ("Leading text "BYTETOBINARYPATTERN, BYTETOBINARY(byte));
For multi-byte types
printf("M: "BYTETOBINARYPATTERN" "BYTETOBINARYPATTERN"\n",
BYTETOBINARY(M>>8), BYTETOBINARY(M));
You need all the extra "s unfortunately. This approach has the efficiency risks of macros (don't pass a function as the argument to BYTETOBINARY) but avoids the memory issues and multiple invocations of strcat in some of the other proposals here.
void print_ulong_bin(const unsigned long * const var, int bits) {
int i;
#if defined(__LP64__) || defined(_LP64)
if( (bits > 64) || (bits <= 0) )
#else
if( (bits > 32) || (bits <= 0) )
#endif
return;
for(i = 0; i < bits; i++) {
printf("%lu", (*var >> (bits - 1 - i)) & 0x01);
}
}
should work - untested.
Print Binary for Any Datatype
//assumes little endian
void printBits(size_t const size, void const * const ptr)
{
unsigned char *b = (char*) ptr;
unsigned char byte;
int i, j;
for (i=size-1;i>=0;i--)
{
for (j=7;j>=0;j--)
{
byte = b[i] & (1<<j);
byte >>= j;
printf("%u", byte);
}
}
puts("");
}
golfed
void p(int s,void* p){int i,j;for(i=s-1;i>=0;i--)for(j=7;j>=0;j--)printf("%u",(*((unsigned char*)p+i)&(1<<j))>>j);puts("");}
test
int main(int argv, char* argc[])
{
int i = 23;
uint ui = UINT_MAX;
float f = 23.45f;
printBits(sizeof(i), &i);
printBits(sizeof(ui), &ui);
printBits(sizeof(f), &f);
return 0;
}
void PrintBinary( int Value, int Places, char* TargetString)
{
int Mask;
Mask = 1 << Places;
while( Places--) {
Mask >>= 1; /* Preshift, because we did one too many above */
*TargetString++ = (Value & Mask)?'1':'0';
}
*TargetString = 0; /* Null terminator for C string */
}
The calling function "owns" the string...:
char BinaryString[17];
...
PrintBinary( Value, 16, BinaryString);
printf( "yadda yadda %s yadda...\n", BinaryString);
Depending on your CPU, most of the operations in PrintBinary render to one or very few machine instructions.