Thread: Counting number of digits in a variable

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    14

    Counting number of digits in a variable

    Dear friends,

    I have an assignment to convert from binary to decimal... I know how to do it but only thing is I need to know how many digits are stored in my variable, for example :

    110101

    so if somehow I know there is 6 numbers I can make my loop and convert it to decimal very easy.

    Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    strlen()
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's only if the variable is stored in a string. If it's stored in an int, you can use a combination of modulus (%) and division (/) in a loop to do the conversion and to figure out how many digits there are. Come to think of it, you can probably do it with just division.

    For example, to count the number of binary digits:
    Code:
    int number = 42;
    int digits = 0;
    while(number) {
        digits ++;
        number /= 2;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    That should be

    Code:
    number /= 10
    not

    Code:
    number /= 2

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by KBriggs View Post
    That should be

    Code:
    number /= 10
    not

    Code:
    number /= 2
    That should be
    Code:
    number /= 2;
    not
    Code:
    number /= 10;

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unless one really wants to deal with negative integers, it may be better to use say, unsigned int, and initialise digits to:
    Code:
    unsigned int digits = number == 0;
    in order to account for the case where the number is 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Haha oops I was thinking in decimal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Making a program take a variable number of arguments
    By subtled in forum C Programming
    Replies: 1
    Last Post: 04-17-2007, 05:38 AM
  4. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  5. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM