Is there a printf converter that prints in binary format?

Keywords: glibc

I can print printf as a hexadecimal or octal number.Is there a format tag printed as binary or any cardinality?

I'm running gcc.

printf("%d %x %o\n", 10, 10, 10); //prints "10 A 12\n"
print("%b\n", 10); // prints "%b\n"

#1st floor

This code should meet your needs for up to 64 bits.I created two functions, pBin & pBinFill.Both do the same thing, but pBinFill fills the leading spaces with fillChar.The test function generates some test data and uses it to print it out.



char* pBinFill(long int x,char *so, char fillChar); // version with fill
char* pBin(long int x, char *so);                   // version without fill
#define kDisplayWidth 64

char* pBin(long int x,char *so)
{
 char s[kDisplayWidth+1];
 int  i=kDisplayWidth;
 s[i--]=0x00;   // terminate string
 do
 { // fill in array from right to left
  s[i--]=(x & 1) ? '1':'0';  // determine bit
  x>>=1;  // shift right 1 bit
 } while( x > 0);
 i++;   // point to last valid character
 sprintf(so,"%s",s+i); // stick it in the temp string string
 return so;
}

char* pBinFill(long int x,char *so, char fillChar)
{ // fill in array from right to left
 char s[kDisplayWidth+1];
 int  i=kDisplayWidth;
 s[i--]=0x00;   // terminate string
 do
 { // fill in array from right to left
  s[i--]=(x & 1) ? '1':'0';
  x>>=1;  // shift right 1 bit
 } while( x > 0);
 while(i>=0) s[i--]=fillChar;    // fill with fillChar 
 sprintf(so,"%s",s);
 return so;
}

void test()
{
 char so[kDisplayWidth+1]; // working buffer for pBin
 long int val=1;
 do
 {
   printf("%ld =\t\t%#lx =\t\t0b%s\n",val,val,pBinFill(val,so,'0'));
   val*=11; // generate test data
 } while (val < 100000000);
}

Output:
00000001 =  0x000001 =  0b00000000000000000000000000000001
00000011 =  0x00000b =  0b00000000000000000000000000001011
00000121 =  0x000079 =  0b00000000000000000000000001111001
00001331 =  0x000533 =  0b00000000000000000000010100110011
00014641 =  0x003931 =  0b00000000000000000011100100110001
00161051 =  0x02751b =  0b00000000000000100111010100011011
01771561 =  0x1b0829 =  0b00000000000110110000100000101001
19487171 = 0x12959c3 =  0b00000001001010010101100111000011

#2nd floor

There is no formatting function in the C standard library to output such a binary file.All formatting operations supported by the printf series are for human-readable text.

#3rd floor

Some runtimes support'%b', although this is not standard.

See also the interesting discussion here:

http://bytes.com/forum/thread591027.html

HTH

#4th floor

There is no standard or portable way.

Some implementations provide itoa() But it won't appear in most cases, and it has some slightly bad interfaces.But the code behind the link should make it easy to format your own program.

#5th floor

Typically, there is no binary conversion specifier in glibc.

Custom conversion types can be added to the printf() function family in glibc.For more information, see register_printf_function .You can add a custom%b transformation for your own use if it simplifies the application code to make it available.

Here's how to implement a custom printf format in glibc Example .

Posted by new_to_php2004 on Mon, 10 Feb 2020 19:04:34 -0800