0

I already have a program that converts hexadecimal numbers into its binary form, the only problem is that it does not accept hexadecimals with float. Here is the code:

    /* HEXADECIMAL TO BINARY */
    #include<stdio.h>
    #define MAX 1000

    int main(){
char hexaDecimal[MAX], *pch;
long int i=0;
clrscr();
printf("Enter any hexadecimal number: ");
scanf("%s",hexaDecimal);
printf("\nEquivalent binary value: ");

while(hexaDecimal[i]){
     switch(hexaDecimal[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;
         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 ",hexaDecimal[i]);
     return 0;
     }
     i++;
}
    getch();
return 0;
}

I tried splitting the string using strtok but it doesn't work.. All I need is for this program to also work with hexadecimal fractions. Thanks for the responses in advance!

3
  • 4
    What kind of input are you entering? What do you mean by "Hexadecimals with float"? Commented Oct 6, 2015 at 15:14
  • 1
    shouldnt you have a case '.' Commented Oct 6, 2015 at 15:15
  • 1
    Does input include e for exponentiation? How about -, +, .? Commented Oct 6, 2015 at 15:22

2 Answers 2

1

Just add to your switch block:

case '.': printf("."); break;
Sign up to request clarification or add additional context in comments.

Comments

0

Just use strtoul defined in stdlib.h you can convert any radix base to decimal from this method including Hexa-decimal.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.