Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

hex – binary converter

Posted by Circuit Negma on February 6, 2006


/*
 *
 * project1.c
 *
 * this is a hex - binary  converter
 *
 * bu hussein, 2004
 */
  
#include <stdio.h>

main()
{
        char hex[100];
        int length;
        char result[100];
        int i;
        
        /* 
         * ask for an input
         */
        printf("Please enter the hex number to be converted: ");
        scanf("%s", hex);
        
        // calc. the length of user input
        length = strlen(hex);
        
        //initilize the results code
        strcpy(result,"");
        
        
        //the actual core of the program
        for (i = 0; i <= length; i = i + 1)
        {
        
        if (hex[i] == '0')
            strcat(result, "0000 ");
        else if (hex[i] == '1')
           strcat(result, "0001 ");
        else if (hex[i] == '2')
            strcat(result, "0010 ");
        else if (hex[i] == '3')
            strcat(result, "0011 ");
        else if (hex[i] == '4')
            strcat(result, "0100 ");
        else if (hex[i] == '5')
            strcat(result, "0101 ");
        else if (hex[i] == '6')
            strcat(result, "0110 ");
        else if (hex[i] == '7')
            strcat(result, "0111 ");
        else if (hex[i] == '8')
            strcat(result, "1000 ");
        else if (hex[i] == '9')
            strcat(result, "1001 ");
        else if (hex[i] == 'A' || hex[i] == 'a')
            strcat(result, "1010 ");
        else if (hex[i] == 'B' || hex[i] == 'b')
            strcat(result, "1011 ");
        else if (hex[i] == 'C' || hex[i] == 'c')
            strcat(result, "1100 ");
        else if (hex[i] == 'D' || hex[i] == 'd')
            strcat(result, "1101 ");
        else if (hex[i] == 'E' || hex[i] == 'e')
            strcat(result, "1110 ");
        else if (hex[i] == 'F' || hex[i] == 'f')
            strcat(result, "1111 "); 
        }
        
        printf(" %s in hex = %s in binary\n",hex,result);   
        fflush(stdin);
        getchar();
}


One Response to “hex – binary converter”

  1. Simon said

    Awesome site!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: