Hi guys! I have a program that makes you choose what kind of number conversion you would like to do between Binary to Octal and Binary to Hexadecimal . Here's the function that converts binary to hexadecimal:

int bintohex(void){
fract=0; bits=0;
 printf("Enter any binary number: ");

    while ((ch=getchar()) != EOF && ch != '.' && ch != '\n') {
    switch(ch) {
            case '0':
                whole = whole * 2;
                break;
            case '1':
                whole = whole * 2 + 1;
                break;
            default:
                printf("Bad input\n");
                return 1;
        }
    }
    printf("Hexadecimal value: %lX", whole);

Before all that, I made these conversions seperately (as in individual programs) and they worked perfectly fine. But now that I put them in one program (the one with the switch statement), it wouldn't even let the user to input a binary and just prints the answer as "0". And when I tried to put scanf("%d", &ch);,it would still answer "0".

Can someone tell me what I did wrong? Or the things that needed to be changed? Thank you very much in advance!

I would firstly take in a validated binary string ... then convert that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.