i need hint...

This is a discussion on i need hint... within the C Programming forums, part of the General Programming Boards category; can someone gimme hint for making program that converts binary number to decimal number? I think if i can find ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    32

    i need hint...

    can someone gimme hint for making program that converts binary number to decimal number?

    I think if i can find number of digits in input, then i can do something.. but how can I find number of digits in input?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>can someone gimme hint for making program that converts binary number to decimal number?
    I'm sure a board search will help. This does seem to be the topic of the week

    >>but how can I find number of digits in input?
    How do you obtain the input? From the user? Did you get it as a string with fgets()? If so, how about using strlen()?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Converting binary to decimal is easy if the binary number is a string. Just do something like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void revstr( char *str )
    {
            char *str_start = str;
            char *str_end = &str[strlen( str ) - 1];
            char chr_hold;
    
            while( str_start < str_end ) {
                    chr_hold = *str_start;
                    *str_start++ = *str_end;
                    *str_end-- = chr_hold;
            }
    }
    
    long bincon( char *bin )
    {
            long dec = 0, base = 1;
    
            revstr( bin );
    
            while( *bin != '\0' ) {
                    if( *bin++ == '1' )
                            dec += base;
                    base <<= 1;
            }
    
            return dec;
    }
    
    int main()
    {
            char bin[] = "1011010" /* Put a binary string here (ex. "1011010") */;
    
            printf( "The decimal value is %ld\n", bincon( bin ) );
    
            return 0;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Post us your code and we help you with it.

    Mr. C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need a hint.
    By hyrule in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2005, 10:31 PM
  2. need hint how to filter the results
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 01-29-2005, 06:27 PM
  3. hi! could anybody help me, a hint at the least
    By alvarorahul in forum C Programming
    Replies: 1
    Last Post: 07-07-2004, 03:37 AM
  4. Just a HINT
    By Diceman in forum C++ Programming
    Replies: 1
    Last Post: 07-25-2003, 06:27 PM
  5. Programming hint required
    By abhijit in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2002, 04:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21