Thread: need help: binary to integer

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    10

    need help: binary to integer

    Hello!
    I have a problem. I have a program, that must convert entered binary number to integer. But this program fails. I have no idea, where should be the mistake.
    Please help !
    sorry for non englis program - it's in estonian, because my home language is estonian.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void bin_dec();
    
    int main(){
        while(1){
        char otsus;
        //Sissejuhatus ja valik
        printf("Mis teisendust on vaja teha:\n");
        printf("d - kahendsusteemist kumnendsusteemi\n");
        printf("v - välju programmist.\n");
        printf("Teie otsus: ");
        scanf("%s", &otsus);
    
        if(strcmp(&otsus, "d") == 0){
                bin_dec();
        }
        else if(strcmp(&otsus, "v") == 0){
                break;
        }
        else{
                printf("Sisestasite sobimatu t2he.\n"); //t2he ehk tähe (täpitähti ta ei näita hästi)
        }
        printf("\n");
        }
    
        system("pause");
        return 0;
    }
    
    
    //Teisendab kahendsüsteemist kümnendsüsteemi
    void bin_dec(){
        char jada[20];
        int i;
        int j = 0;
        int arv = 0;
        printf("Sisestage number (1'd ja 0'id): ");
        scanf("%s", &jada);
        printf("\nArv %s on kumnendsusteemis ", jada);
        for(i = strlen(jada); i >= 0; i--){
            arv = arv + atoi(jada[i]) * pow(2, j);
            printf("%d", arv);
            j++;
            }
        }
    Last edited by P6nn; 05-28-2012 at 06:57 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    5
    atoi doesn't work with char arrays
    so do (jada[i]-'0'),this will help

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, otsus is supposed to be a string in your logic, but you declare it as one character.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Some other things in bin_dec()

    - include <string.h> and <stdlib.h>
    - initialize i to strlen(jada) -1, or you will be off by one.
    - move your printf to outside the loop, unless you want to print the value of arv in each iteration

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    10
    Okay!
    Now it works fine! Thanks!
    Last edited by P6nn; 05-28-2012 at 12:05 PM.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    10
    I have now another problem.
    I have another program too, but this converts entered integer to binary. Everything works fine, but it prints very strange number to me.
    When I ester 2, the answer should be 10, but it gives me lot of strange numbers before it: 557310410 (every time gives different numbers before 10)

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void dec_bin();
    
    int main(){
        char otsus = 0;
        while(otsus !=  'v'){
        //Sissejuhatus ja valik
        printf("Mis teisendust on vaja teha:\n");
        printf("b - Kumnendsusteemist kahendsusteemi\n");
        printf("v - V2lju programmist\n");
        printf("Teie otsus: ");
        scanf("%s", &otsus);
    
        switch(otsus){
           case 'b':
                   dec_bin();
                   printf("\n");
                   break;
           case 'v':
                     break;
          default:
                     printf("\nSisestasite vale t2he\n\n");
                     break;
          }
        }
    
        system("pause");
        return 0;
    }
    
    //Teisendab kümnendsüsteemist kahendsüsteemi
    void dec_bin(){
        int str[200];
        int n, x, k;
        int i = 0;
        printf("\nSisestage number:");
        scanf("%d", &n);
        x = n;
    
        while(n >= 1){
            int number = n%2;
            str[i] = number;
            n = n/2;
            i += 1;
        }
        printf("Arv %d on kahendsusteemis ", x);
        for(k = i ; k >= 0 ; k --){
              printf("%d", str[k]);
              }
        printf("\n");
    }

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are doing the exact same thing. Trying to read a string into a char.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to integer
    By astrogal in forum C Programming
    Replies: 2
    Last Post: 10-18-2011, 03:49 AM
  2. Long integer to binary pattern
    By mark522 in forum C Programming
    Replies: 3
    Last Post: 12-17-2009, 06:03 AM
  3. integer to binary
    By rs07 in forum C Programming
    Replies: 2
    Last Post: 09-17-2008, 12:18 AM
  4. How to display integer to binary format?
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2005, 11:32 PM
  5. Recursive function that changes an integer to binary
    By advocation in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2005, 07:08 PM