简体   繁体   中英

Binary to decimal converter in C doesn't work after a certain number

I've tried making a positive binary to decimal number converter using C, but when i try inputting values higher than 1110011010110001010111 (3779671 in decimal) the program always returns that exact number. My current assignment requires that it works on binary numbers up to 111111111111111111111111111111 (1073741823).

So far, i've tried changing the variable types to any other larger sizes possible, but it doesn't seem to work. Here's the current code:

#include <math.h>

void main()
{
unsigned long long int bi, de = 0;    
unsigned long long int x = 0, bases;  

scanf("%llu", &bi); 

for(x=0 ; bi>0 ; x++, bi=bi/10){
    bases = bi % 10;              
    de = de + bases * pow(2,x);

}                       

printf("%llu", de); // imprime o correspondente em decimal

}

Thanks for the help in advance.

You don't need all that indexing and adding. You can simply shift in the bits from the right:

#include <stdio.h>
#include <string.h>

unsigned long long int bin2dec(const char *string)
{
    unsigned long long int value = 0;
    while (*string != '\0')
    {
        // make room for the next bit by shifting what is there already
        value <<= 1;
        // *string != '0' gives 1 if the current character is not '0', else 0
        value |= *string != '0';
        string++;
    }
    return value;
}

int main(void)
{
    //                     7  F   C   F   F   4   F   A   F   F   F
    const char * binary = "1111111110011111111010011111010111111111111";
    unsigned long long int decimal = bin2dec(binary);
    printf("%llX\n", decimal);
    return 0;
}

You can't read the binary number 111111111111111111111111111111 and put it in a unsigned long long integer because the limit for a unsigned long long int is 18446744073709551615, so you need to read the binary number as a string of characters and then convert each character to a numeric digit instead:

#include <stdio.h>
#include <string.h>
#include <math.h>

unsigned long long int bin2dec(const char *string, const size_t size)
{
    unsigned long long int bit, value = 0;
    for(size_t index=0;index<size;index++)
    {
        // moving from the end to the beginning, get a character from the string
        // and convert it from a character containing a digit to a number
        bit = string[size-index-1]-'0';

        // in the original question this was: value += bit*pow(2,index);
        // but we can just do this and get the same effect
        // without multiplication or library function
        value += bit<<index;
    }
    return value;
}

int main()
{
    const char * binary = "111111111111111111111111111111";
    unsigned long long int decimal = bin2dec(binary, strlen(binary));
    printf("%llu\n",decimal);
    return 0;
}

Try it here: https://onlinegdb.com/Skh7XKYUU

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM