0 Replies - 435 Views - Last Post: 21 January 2007 - 03:45 PM Rate Topic: -----

#1 chris.tkd   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 26-September 06

Hex and Binary to Decimal

Posted 21 January 2007 - 03:45 PM

Description: compiled with visual studio 2005converts hexadecimal and binary numbers to decimal.
/* Converts binary and hex to decimal The input for a hex number is as follows *
 * 0xffffffff can be up to 8 digits, this can be changed by changing the value *
 * of char array ie char number[100] will calculate up to 98 digits long. For  * 
 * binary format is 0z11111111. Created by chris.tkd 22/1/07, *all lower case* */

#include <stdio.h>//standard header
#include <math.h>//needed for power

void binToDec(char bin[]);//prototypes
void hexToDec(char bin[]);

int main(void)
{
     char number[10], *p;//allows up to 8 bits of actual data ie 0z or 0x then 8 digits
          
     printf("nEnter in the number with a preceding 0x if Hex 0z for Binary: ");
     scanf("%s", &number);//get input

     p = number;//point the char pointer to the char array

     if(*p == '0')//if first char is 0
     {
          p++;//increment
          if(*p == 'x')//if second letter is x its a hex number
               hexToDec(number);//so call hexToDec function
          else if(*p == 'z')//else if its a z its a binary number
               binToDec(number);//so use the binary to decimal function
          else//else its invalid input ie the 0 isnt followed by an x or z
               printf("nInvalid Format");
     }

     else//else its invalid input dosnt start with 0
          printf("nInvalid Format");

     return 0;     
}
/*function to convert binary to decimal*/
void binToDec(char bin[])
{
     char *ptr;
     int sum = 0;
     int value;
     int power = -1;

     ptr = bin;//point the pointer to the first digit of the number ie 0
     ptr += 2;//first digit after the 0z ie 1 or 0 

     /*this calculates the length of the number entered so any length value can be converted*/
     while(*ptr != (char)NULL)//while pointer is not null count a new char
     {
          ptr++;//increment the pointer
          power++;//increment the power
     }

     ptr = &bin[2];//point to the charachter after the 0z
     
     while(*ptr != (char)NULL)//while the pointer is not at null
     {
          if(*ptr == '1')//if pointer points to 1
               value = 1;//the value is 1
          else if(*ptr == '0')//else if it points to 0
               value = 0;//the value is 0
          else//An input error has occured ie not 0 or 1
          {
               printf("Value is %c = &d, not 0 or 1 for binary", *ptr, *ptr);
               exit(1);
          }

          sum += (int)pow((int)2,(int)power) * value;//sum = 2^ of what ever the length is * value if its not 0
          power--;//decrement power
          ptr++;//go to next digit
     }

     printf("nBinary: %s, Decimal: %d", bin, sum);
}

/*function to convert hexadecimal to decimal*/
void hexToDec(char bin[])
{
     char *ptr;//pointer to a char
     int value = 0; // the value of the current hex char pointed to
     int sum = 0;// the running sum
     int power = -1;//power is what base 16 is raised to starts at -1 to count for 0

     ptr = bin;//set the pointer to point at start of string ie 0xabcd
     ptr += 2;//first digit after the 0x ie a

     /*use this to work out the length of the hex number so any length
     input can be calculated power is the length of the string*/
     while(*ptr != (char)NULL)//while pointer is not null count a new char
     {
          ptr++;
          power++;
     }
     
     ptr = &bin[2];//sets pointer to point at first value after 0x ie a
     
     while(*ptr != (char)NULL)//while pointer is not equal to null
     {
          /*this switch statement is used to computer the current value
          that the pointer points to using hex alphabet*/
          switch(*ptr)
          {
          case '0': value = 0; break;
          case '1': value = 1; break;
          case '2': value = 2; break;
          case '3': value = 3; break;
          case '4': value = 4; break;
          case '5': value = 5; break;
          case '6': value = 6; break;
          case '7': value = 7; break;
          case '8': value = 8; break;
          case '9': value = 9; break;
          case 'a': value = 10; break;
          case 'b': value = 11; break;
          case 'c': value = 12; break;
          case 'd': value = 13; break;
          case 'e': value = 14; break;
          case 'f': value = 15; break;
          default: printf("Input Error not 0-9 or a-f"); exit(1);
          }
          
          sum += pow(16,power)*value;//this calculates the total until pointer points to null
          ptr++;//moves the pointer along to the next char
          power--;//decrements the power
     
     }
     printf("nHex: 0x%x, Decimal: %d ", sum, sum);
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1