Thread: Binary to decimal converter: Bug on greater than 10 digits

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    33

    Binary to decimal converter: Bug on greater than 10 digits

    Everything works fine until I enter a binary number that has more than 10 digits. In that case, if I enter eleven '1's, sum will equal 1024 (which is the 10th power of 2) and its not adding the rest. I used a print statement to show this. Whats going on?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
            char number[50];
            puts("Enter Binary Number: ");
            scanf("%s", number);
            int length = strlen(number);
            int remainder = 0, div = 1, idx = 0,    
            array[length], i, l, num = atoi(&number);
    
            for(i = 1; i < length; i++){
               div *= 10;
            }
    
            while(num){
               remainder = num/div;
               num = num % div;
               div = div/10;
               array[idx] = remainder;
               idx++;
            }
            int j, n = 1, k = 0, compare[length];
        
            for(j = 1; j <= length; j++){
               compare[k] = n=n>1? n*2:j;
               k++;
            }
    
           int sum = 0, r = 0;
           for(j = length-1; j >= 0; j--){
              if(array[j] == 1){
                  sum += compare[r];
              }
                r++;
           printf("test: %d\n", sum); //<--- test
           }
           printf("Decimal: %d\n", sum);
    
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well the code didn't compile cleanly:
    Code:
    C:\Users\Josh2\Documents\foo\main.c||In function 'main':|
    C:\Users\Josh2\Documents\foo\main.c|10|warning: passing argument 1 of 'atoi' from incompatible pointer type|
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdlib.h|304|note: expected 'const char *' but argument is of type 'char (*)[50]'|
    C:\Users\Josh2\Documents\foo\main.c|10|warning: unused variable 'l'|
    ||=== Build finished: 0 errors, 2 warnings ===|
    That's after I included the necessary <stdlib.h> library for the function atoi().

    I fixed that by changing how you make the atoi call to atoi(number);

    Then you have a much bigger problem. I started debugging your program, and then I remembered that div() was a C function, so that explained a lot of strange behavior. I enabled an extra warning (-Wshadow) and got this:
    Code:
    C:\Users\Josh2\Documents\foo\main.c||In function 'main':|
    C:\Users\Josh2\Documents\foo\main.c|9|warning: declaration of 'div' shadows a global declaration|
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdlib.h|396|warning: shadowed declaration is here|
    C:\Users\Josh2\Documents\foo\main.c|10|warning: unused variable 'l'|
    ||=== Build finished: 0 errors, 3 warnings ===|
    In general you need to address your code's warnings from the compiler.
    Last edited by whiteflags; 12-01-2012 at 12:20 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
            int j, n = 1, k = 0, compare[length];
         
            for(j = 1; j <= length; j++){
               compare[k] = n=n>1? n*2:j;
    Fee fi fo fum, I smell a buffer overrun.

    (welcome to Salem's poetry corner)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    33
    I never got those warnings. Everything complied fine for me, but I made those changes you mentioned anyway.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by atac View Post
    I never got those warnings. Everything complied fine for me, but I made those changes you mentioned anyway.
    Your compiler sucks. Get something from this century: Code::Blocks

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by whiteflags View Post
    Your compiler sucks. Get something from this century: Code::Blocks
    MingW would be sufficient .... it is the compiler that generates warnings, not an IDE like Code::Blocks.

    It is also necessary to use the compiler in a way that generates warnings about suspicious code constructs. Most compilers (modern or otherwise) are configured by default so they do not issue all the warnings they could.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by grumpy View Post
    MingW would be sufficient .... it is the compiler that generates warnings, not an IDE like Code::Blocks.
    True but Code::Blocks comes with a compiler so I don't have to explain how mingw is installed.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by atac View Post
    Whats going on?
    You have an integer overflow:
    Code:
    $ gdb -q ./foo
    Reading symbols from foo...done.
    (gdb) b 16
    Breakpoint 1 at 0x80485f1: file foo.c, line 16.
    (gdb) r
    Starting program: ./foo 
    Enter Binary Number: 
    1111111111
    
    Breakpoint 1, main () at foo.c:17
    17            while(num){
    (gdb) p divisor
    $1 = 1000000000
    (gdb) r
    The program being debugged has been started already.
    Start it from the beginning? (y or n) y
    Starting program: ./foo 
    Enter Binary Number: 
    11111111111
    
    Breakpoint 1, main () at foo.c:17
    17            while(num){
    (gdb) p divisor
    $2 = 1410065408
    When you enter a 11-digit binary number your "div" (which I have changed to "divisor") is exceeding the maximum integer value on your system.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to C - decimal to hexadecimal converter
    By emblem4ever in forum C Programming
    Replies: 20
    Last Post: 01-23-2010, 04:08 PM
  2. Hex String to Decimal converter
    By wrex in forum C Programming
    Replies: 16
    Last Post: 11-05-2008, 06:06 PM
  3. Decimal to Hex Converter
    By rocketman03 in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:50 AM
  4. Help with a decimal to binary converter
    By danielerasmus in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2008, 11:37 AM
  5. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM