This blog is under construction

Friday 5 July 2013

C program to convert alphanumeric to binary

Write a C program to convert Alphanumeric characters to binary values.


  #include <stdio.h>

  int charToBinary(int input) {
        int result = 0, i = 1, mod;

        /* convert decimal to binary format */
        while (input > 0) {
                mod = input % 2;
                result = result + (i * mod);
                input = input / 2;
                i = i * 10;
        }

        /* print the resultultant binary value */
        return(result);
  }

  int main() {
        int char1 = '0', char2;
        printf("      Numeric  BINARY\n");
        while (char1 <= '9') {
                printf("\t%c\t%6d\n", char1, charToBinary(char1));
                char1++;
        }
        printf("\n\n");
        printf("  CAPSALPHA     BINARY   SMALLALPHA  ASCII\n");
        char1 = 'A', char2 = 'a';
        while (char1 <= 'Z') {
                printf("\t%c\t%6d", char1, charToBinary(char1));
                printf("\t%4c\t%12d\n", char2, charToBinary(char2));
                char1++;
                char2++;
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
      Numeric  BINARY
0 110000
1 110001
2 110010
3 110011
4 110100
5 110101
6 110110
7 110111
8 111000
9 111001


  CAPSALPHA  BINARY   SMALLALPHA  ASCII
A 1000001   a     1100001
B 1000010   b     1100010
C 1000011   c     1100011
D 1000100   d     1100100
E 1000101   e     1100101
F 1000110   f     1100110
G 1000111   g     1100111
H 1001000   h     1101000
I 1001001   i     1101001
J 1001010   j     1101010
K 1001011   k     1101011
L 1001100   l     1101100
M 1001101   m     1101101
N 1001110   n     1101110
O 1001111   o     1101111
P 1010000   p     1110000
Q 1010001   q     1110001
R 1010010   r     1110010
S 1010011   s     1110011
T 1010100   t     1110100
U 1010101   u     1110101
V 1010110   v     1110110
W 1010111   w     1110111
X 1011000   x     1111000
Y 1011001   y     1111001
Z 1011010   z     1111010



No comments:

Post a Comment