Thread: Converting binary string to decimal

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Converting binary string to decimal

    The following was given as an example of how to convert a binary string to an int.

    Code:
    int bstr_to_dec(const char * str)
    {
        int val = 0;
        
        while (*str != '\0')
            val = 2 * val + (*str++ - '0');
        return val;
    }
    I have no trouble going through it step by step and seeing that it does indeed work, but I'm having trouble understanding why it works and what the principle behind it is. Can anyone explain?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. converting char '0' or '1' to int is done like (ch - '0') wich gives you int 0 or 1

    2. as you know decimal number like abcd could be written as d + 10*(c+10*(b+10*a)

    same goes for binary numbers just intead of 10 - 2 is used
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Perhaps it would save time if you could say which bits are most confusing for you?
    E.g. Does the multiply by 2 make sense? Would it help to know that it is the same as (val << 1), a shift left by 1 bit?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by iMalc View Post
    Perhaps it would save time if you could say which bits are most confusing for you?
    E.g. Does the multiply by 2 make sense? Would it help to know that it is the same as (val << 1), a shift left by 1 bit?
    I guess I'm just having trouble thinking about the idea of a number represented in the form that vart gives, i.e. d + 10*(c+10*(b+10*a) for base 10. The only way I've really thought about it is (d * 10 pow 3) + (c * 10 pow 2) + (b * 10) + d. And when I've written a little binary to decimal function myself in the past, I used this method in starting from the last digit and working my way back in this fashion. I know you're going to tell me that the first expression is exactly the same as my version, but being quite mathematically rusty it's not immediately apparent to me why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM