Platinum Partner

C Convert Buffer To Binary String

// Sometimes one wants to see data in binary
// This method converts an arbitrary buffer to 1's and 0's


#include 
#include 
#include 
#include 

char *getBufferAsBinaryString(void *in)
{
	int pos=0;
	char result;
	char bitstring[256];
	memset(bitstring, 0, 256);
	unsigned int *input= (unsigned int *)in;
	for(int i=31;i>=0;i--)
	{
		if (((*input >> i) & 1)) result = '1';
		else result = '0';
		bitstring[pos] = result;
		if ((i>0) && ((i)%4)==0)
		{
			pos++;
			bitstring[pos] = ' ';
		}
		pos++;
	}
	return bitstring;
}
int main(int argc, char* argv[])
{
	int i=53003;
	char buffer[1024];
	char *s=getBufferAsBinaryString(&i);
	strcpy(buffer, s);
	printf("%s\n", buffer);
}


produces this output:
0000 0000 0000 0000 1100 1111 0000 1011
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}