Thread: Binary number from string to int array help

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    73

    Binary number from string to int array help

    The following code is supposed to read in input from the keyboard, encode it to a string, and then take each digit from the string and put it into an integer array for the purposes of adding two binary numbers together.

    See below for the test input and result.

    Can anyone explain why this is so and how to fix it? I want to make sure I'm getting the right numbers in the right places before I move forward, as it will make the rest of the work that much easier.

    Thanks!

    INPUT: 11010010
    OUTPUT: 0034FB90

    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        int b1[8], b2[8], b3[9];
        string s1, s2;
    
    
        cout << "Please enter the first binary number to add:  ";
        cin >> s1;
        cout << endl << endl;
    
    
        for(int i=0; i<8; i++)
            b1[i]=s1[i];
    
    
        cout << b1;
    
    
        system("pause");
        return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    s1[i] contains character '1'

    to convert it to integer 1 you need to do s1[i] - '0'
    Code:
    b1[i]=s1[i] - '0';
    Of course you better use isdigit() before conversion to validate the user input
    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
    Registered User
    Join Date
    Nov 2012
    Posts
    73
    Quote Originally Posted by vart View Post
    s1[i] contains character '1'

    to convert it to integer 1 you need to do s1[i] - '0'
    Code:
    b1[i]=s1[i] - '0';
    Of course you better use isdigit() before conversion to validate the user input
    I do that and I get 0034F914.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First look at this snippet:

    Code:
        int b1[8];
    ...
        cout << b1;
    You're not printing the value contained in your array, you're printing the address of the array.

    When you convert the binary number to an int you need to add the power, for example with your entry "1101 0010" it would be 21 + 24 + 26 + 27. Which would be:
    Code:
    + 128  1000 0000 
    +  64  0100 0000
    +  16  0001 0000
    +   2  0000 0010 = 210 or 0xD2.

    Jim
    Last edited by jimblumberg; 09-22-2013 at 04:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert 8-digit Binary Number to decimal number
    By yongsheng94 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2013, 09:47 AM
  2. Typing decimal number and cover it binary number!
    By Kazumi in forum C Programming
    Replies: 32
    Last Post: 04-16-2011, 07:21 PM
  3. binary search function on string array
    By gandolf989 in forum C Programming
    Replies: 6
    Last Post: 10-02-2007, 01:47 PM
  4. binary number in array? urgently help
    By shteo83 in forum C Programming
    Replies: 4
    Last Post: 02-25-2006, 12:28 PM
  5. storing a string of binary into an array
    By daisy_polly in forum C Programming
    Replies: 5
    Last Post: 02-23-2006, 02:45 PM