Thread: binary number to decimal

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    12

    binary number to decimal

    Hi all.
    I am very new to programming and have been working on a program that can receive decimals or binary numbers and convert them. The decimal --> binary works fine. For some reason I cannot figure out I cannot get the "BinaryToDecimal" function to perform. By putting a "printf" into the for-loop I found that it only goes through one iteration and even then doesn't produce even a partially correct answer. Please advise!
    Thanks a bunch.
    P.S - since I am new please don't give elaborate or advanced ideas - I am in a course so they won't want us to use to many things we haven't learned yet..

    Code:
    #include <stdio.h>#include <string.h>
    #include <math.h>
    
    
    char* ReverseString (char _result[])
    {
        int start, end, length = strlen(_result);
        char swap;
        
        for (start = 0, end = length-1; start < end; start++, end--)
        {
            swap = *(_result + end);
            *(_result + end) = *(_result + start);
            *(_result + start) = swap;
        }
        
        return _result;
    }
    
    
    
    
    int IntegerToBinary (int _num, char* _str)
    {
        int i;
        int devided = _num;
            
        for (i = 0; devided > 0 ; i++)
        {
            *(_str + i) = devided % 2 + '0';
            printf("%c %d\n", _str[i],i);
            devided /=2;
        }
        *(_str + i) = '\0';
        ReverseString(_str);
        return 1;
    }
    
    
    int BinaryToDecimal (char _binary[])
    {
        int i;
        int newnum = 0;
        int length = strlen(_binary);
    
    
        ReverseString(_binary);
            
        for (i = 0; i < length; i++);
        {
            newnum += (*(_binary + i) + '0') * (pow(2, i));
            printf("newnum - %d , length - %d",newnum, length);
        }
        
        return newnum;
    
    
    }
    
    
    
    
    int main()
    {
        char c;
        int num;
        char str[1000];
        
        puts("a) Integer to binary (b) Binary to Integer");
        c= getchar();
        
        switch (c)
        {
            case 'a':
            {
                puts("Please type a decimal integer to change to binary");
                scanf("%d", &num);
                
                IntegerToBinary(num, str);
                printf("Your number represented in binary is: %s\n", str);
                break;
            }
            case 'b':
            {
                puts("Please type a binary number to change to integer");
                scanf("%s", str);
                num = BinaryToDecimal(str);
                printf("Your binary number represented in decimals is: %d\n", num);
                break;
            }
            default :
            {
                puts("Try again. a or b");
                break;
            }
        
        }
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    228
    Hi Amos Bordowitz.

    First of all, your loop does not run only once, it actually runs through 5 iterations as it should, only you accidently typed ';' right after it,
    and so instead of running the code lines inside the brackets every iteration, it does nothing but incrementing the index i and checking the termination condition.
    so:
    Code:
    for (i = 0 ; i < length; i++)
    instead of:
    Code:
    for (i = 0 ; i < length; i++);
    And finally, try:
    Code:
    newnum+=(*(_binary+i)-'0')*pow(2,i);
    instead of:
    Code:
    newnum+=(*(_binary+i)+'0')*pow(2,i);
    Hope this could help.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    12
    Quote Originally Posted by Absurd View Post
    Hi Amos Bordowitz.

    First of all, your loop does not run only once, it actually runs through 5 iterations as it should, only you accidently typed ';' right after it,
    and so instead of running the code lines inside the brackets every iteration, it does nothing but incrementing the index i and checking the termination condition.
    so:
    Code:
    for (i = 0 ; i < length; i++)
    instead of:
    Code:
    for (i = 0 ; i < length; i++);
    And finally, try:
    Code:
    newnum+=(*(_binary+i)-'0')*pow(2,i);
    instead of:
    Code:
    newnum+=(*(_binary+i)+'0')*pow(2,i);
    Hope this could help.
    YUP!!! Thanks a million!
    You rule @absurd
    mods - feel free to mark this as solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert a binary number into a decimal number
    By HTHVampire in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2013, 09:53 AM
  2. Convert 8-digit Binary Number to decimal number
    By yongsheng94 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2013, 09:47 AM
  3. Typing decimal number and cover it binary number!
    By Kazumi in forum C Programming
    Replies: 32
    Last Post: 04-16-2011, 07:21 PM
  4. Binary Number to Decimal
    By theCanuck in forum C++ Programming
    Replies: 12
    Last Post: 02-09-2011, 11:25 PM
  5. Replies: 9
    Last Post: 10-07-2006, 05:37 AM

Tags for this Thread