Binary to decimal in c

后端 未结 5 609
Happy的楠姐
Happy的楠姐 2020-12-19 20:51

I have a simple code to convert binary to decimal numbers. In my compiler, the decomposition works just fine for number less than 1000, beyond the output is always the same

相关标签:
5条回答
  • 2020-12-19 21:23

    After a little experimentation, I think that your program is intended to accept a number consisting of 1's and 0's only as a base-10 number (the %d reads a decimal number). For example, given input 10, it outputs 2; given 1010, it outputs 10; given 10111001, it outputs 185.

    So far, so good. Unfortunately, given 1234, it outputs 15, which is a little unexpected.

    If you are running on a machine where int is a 32-bit signed value, then you can't enter a number with more than 10 digits, because you overflow the limit of a 32-bit int (which can handle ±2 billion, in round terms). The scanf() function doesn't handle overflows well.

    You could help yourself by echoing your inputs; this is a standard debugging technique. Make sure the computer got the value you are expecting.

    I'm not going to attempt to fix the code because I think you're going about the problem in completely the wrong way. (I'm not even sure whether it's best described as binary to decimal, or decimal to binary, or decimal to binary to decimal!) You would do better to read the input as a string of (up to 31) characters, then validate that each one is either a 0 or a 1. Assuming that's correct, then you can process the string very straight-forwardly to generate a value which can be formatted by printf() as a decimal.

    0 讨论(0)
  • 2020-12-19 21:39
    #include <stdio.h>  //printf
    #include <string.h> //strlen
    #include <stdint.h> //uintX_t or use int instead - depend on platform.
    
    /* reverse string */
    char *strrev(char *str){
        int end = strlen(str)-1;
        int start = 0;
    
        while( start<end ){
            str[start] ^= str[end];
            str[end]   ^= str[start];
            str[start] ^= str[end];
            ++start;
            --end;
        }
        return str;
    }
    
    
    /* transform binary string to integer */
    uint32_t binstr2int(char *bs){
        uint32_t ret = 0;
        uint32_t val = 1;
    
        while(*bs){
           if (*bs++ == '1') ret = ret + val;
           val = val*2;
        }
        return ret;
    }
    
    int main(void){
        char binstr[] = "1010101001010101110100010011111"; //1428875423
        printf("Binary: %s, Int: %d\n", binstr, binstr2int(strrev(binstr)));
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-19 21:41

    Yes try this :

    #include <stdio.h>
    int main(void) 
    { 
    char bin; int dec = 0;
    
    while (bin != '\n') { 
    scanf("%c",&bin); 
    if (bin == '1') dec = dec * 2 + 1; 
    else if (bin == '0') dec *= 2; } 
    
    printf("%d\n", dec); 
    
    return 0;
    
    }
    
    0 讨论(0)
  • 2020-12-19 21:41

    Most likely this is because you are using an int to store your binary number. An int will not store numbers above 2^31, which is 10 digits long, and 1023 is the largest number you can get with 10 binary digits.

    It would be much easier for you to read your input number as a string, and then process each character of the string.

    0 讨论(0)
  • 2020-12-19 21:43

    Shift left is the same than multiply by 2 and is more efficient, so I think it is a more c-like answer:

    #include <stdio.h>
    #include <stdlib.h>
    
    int bin2int(const char *bin) 
    {
        int i, j;
        j = sizeof(int)*8;
        while ( (j--) && ((*bin=='0') || (*bin=='1')) ) {
            i <<= 1;
            if ( *bin=='1' ) i++;
            bin++;
        }
        return i;
    }
    
    int main(void) 
    { 
        char* input = NULL;
        size_t size = 0;
    
        while ( getline(&input, &size, stdin) > 0 ) {
            printf("%i\n", bin2int(input)); 
        }
        free(input);
    }
    
    0 讨论(0)
提交回复
热议问题