How to print binary to stdout in C


In standard C, there is no format specifier (such as %d, %x, etc..) to printf that allows printing binary representation of data.


Example for printing binary data

This code snippet shows how to print binary representation of bytes in C.

#include <stdint.h>
#include <stdio.h>
 
void
print_byte(uint8_t byte)
{
        uint8_t         i;
        for (i = 0; i < 8; i++)
                if (byte & (1 << i))
                        printf("1");
                else
                        printf("0");
}
 
 
int
main()
{
 
print_byte(4);
 
return 0;
}

Compile and run to see the binary representation of the integer 4 printed in binary.

 $cc test.c
 $./a.out
  00100000

If you need to display a whole string in binary, simply use the function print_byte for all characters of the string.


More coding articles

Using the Gtags source code tag system with vim
Using vim file buffers
Declaring and initializing an array of structures in C
Buffered (fread,fwrite) Vs non buffered (read, write) i/o
Code review utility for software porting projects
Essential GDB debugging commands
Computing number of data blocks using bitwise operations
Array of strings example in C
Simple makefile example using built-in default rules
Difference between signed char and unsigned char
Conditional Inclusion of C functions
Perl http post example
Program memory usage
The GCC pre-processor
How to create a new MySQL database in Debian/Ubuntu
How to reduce a library size using etrace
Method for studying keyword density in top ranking pages
Method for studying keyword density in top ranking pages
Parsing XML files in Perl using XML::Simple
5 Essential vim plugins for programmers
Using mingw and libtool to cross-compile static libraries
Browse Kerberos source in HTML
Howto test performance of data alignment
Memory management mistakes to avoid
Howto extract e-mail MIME attachments using Perl and uudecode
Vim auto-completion howto
ASN.1 DER encoding and decoding in C - example/tutorial
How to print binary to stdout in C
Custom transport for Kerberos initial authentication made easy in Heimdal
How to implement state-machine / automata in C
Libxml2 example for howto parse XML files in C
Advanced Network Programming
Howto add new EAP methods to wpa_supplicant and hostapd
Script for creating Perl CPAN packages
FreeBSD divert sockets
Why you should use stdint.h
Perl server sockets example
Error handling paradigm for C programs
Autoconf and automake for libraries
DES encryption with OpenSSL: example in C
Javascript example using flickr api to fetch random images with tags
10 essential tools for Ph.D students in Information Science
gettimeofday and localtime millisecond timestamps example
Syslog simple example
Howto create Debian/Ubuntu (.deb) packages with dpkg
Password-less Rsync over ssh howto
Release engineering and methodology with CVS branching and merging
Autotools by example


Labels: , Wireless Internet Security Coding Network Monitoring

Comment

Enter your comment (wiki syntax is allowed):
YBCIG

Wireless Internet Security Performance RADIUS server Wireless Internet Security Performance RADIUS server