Question: Converting data
Posted by kevaughn
Last Updated: July 01, 2014
  1037

Anyone knows a method to convert hexadecimal data to binary and from that binary data to decimal. So the output will be in decimal format. Data is in a txt file where i used the bufferedreader method to read the hexadata.
   
  
 
 
   

 
 
 binary data to decimal
int binary_to_decinal (char bin[]){
 
     int c,x=0,i,factor=1;
     int decimal=0;
     int len;
 
     len = strlen (bin)-1;
 
     for (i=0;i<=len;len--){
            if (bin[len]==49){
               c=bin[len]-48;}
            else if (bin[len]==48){
               c=bin[len]-48;}
         decimal = decimal + c*factor;
         factor = factor*2;
     }     
    return decimal; 
}
 
 binary data to decimal
void hexadecimal_to_binary()
{
    char binaryNum[100],hexaDec[100];
     int i=0;
    printf("Enter hexadecimal number: ");
    scanf("%s",hexaDec);
    printf("\The binary value of the hexadecimal number is: ");
      while(hexaDec[i])
     {
        switch(hexaDec[i])
        {
             case '0': printf("0000"); break;
             case '1': printf("0001"); break;
             case '2': printf("0010"); break;
             case '3': printf("0011"); break;
             case '4': printf("0100"); break;
             case '5': printf("0101"); break;
             case '6': printf("0110"); break;
             case '7': printf("0111"); break;
             case '8': printf("1000"); break;
             case '9': printf("1001"); break;
             case 'A': printf("1010"); break;
             case 'B': printf("1011"); break;
             case 'C': printf("1100"); break;
             case 'D': printf("1101"); break;
             case 'E': printf("1110"); break;
             case 'F': printf("1111"); break;
             default:  printf("\nInvalid hexadecimal digit %c ,hexaDec[i]);
             return 0;
         }
         i++;
     }
}