C Program to Convert Ascii to Binary

Convert ASCII to binary in C : The following c program asciiValueToBinary() function converts ASCII value of all characters from '0' to '~' into equivalent binary value.

C Program to Convert Ascii to Binary

/* ascii to binary in C*/

#include<stdio.h>/

int asciiValueToBinary(int asciiInput)
{
	int res = 0, i = 1, rem;
        
	while (asciiInput > 0)
	{
		rem = asciiInput % 2;
		res = res + (i * rem);
		asciiInput = asciiInput / 2;
		i = i * 10;
	}

	return(res);
}

int main()
{
	int ch = '0';
       
	printf("\t Character \t ASCII \t  Binary\n");
        
	while (ch <= '~')
	{
		printf("\t %c \t\t\t   %d \t\t\t %d \n", ch, toascii(ch), asciiValueToBinary(toascii(ch)));
                ch++;
	}

	return 0;
}

/* Output of above code:-


Character ASCII  Binary
  0       48     110000 
  1       49     110001 
  2       50     110010 
  3       51     110011 
  4       52     110100 
  5       53     110101 
  6       54     110110 
  7       55     110111 
  8       56     111000 
  9       57     111001 
  :       58     111010 
  ;       59     111011 
  <       60     111100 
  =       61     111101 
  >       62     111110 
  ?       63     111111 
  @       64     1000000 
  A       65     1000001 
  B       66     1000010 
  C       67     1000011 
  D       68     1000100 
  E       69     1000101 
  F       70     1000110 
  G       71     1000111 
  H       72     1001000 
  I       73     1001001 
  J       74     1001010 
  K       75     1001011 
  L       76     1001100 
  M       77     1001101 
  N       78     1001110 
  O       79     1001111 
  P       80     1010000 
  Q       81     1010001 
  R       82     1010010 
  S       83     1010011 
  T       84     1010100 
  U       85     1010101 
  V       86     1010110 
  W       87     1010111 
  X       88     1011000 
  Y       89     1011001 
  Z       90     1011010 
  [       91     1011011 
  \       92     1011100 
  ]       93     1011101 
  ^       94     1011110 
  _       95     1011111 
  `       96     1100000 
  a       97     1100001 
  b       98     1100010 
  c       99     1100011 
  d       100     1100100 
  e       101     1100101 
  f       102     1100110 
  g       103     1100111 
  h       104     1101000 
  i       105     1101001 
  j       106     1101010 
  k       107     1101011 
  l       108     1101100 
  m       109     1101101 
  n       110     1101110 
  o       111     1101111 
  p       112     1110000 
  q       113     1110001 
  r       114     1110010 
  s       115     1110011 
  t       116     1110100 
  u       117     1110101 
  v       118     1110110 
  w       119     1110111 
  x       120     1111000 
  y       121     1111001 
  z       122     1111010 
  {       123     1111011 
  |       124     1111100 
  }       125     1111101 
  ~       126     1111110 


*/