# Thread: Binary number from string to int array help

1. ## 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;

cin >> s1;
cout << endl << endl;

for(int i=0; i<8; i++)
b1[i]=s1[i];

cout << b1;

system("pause");
return 0;
}```

2. 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

3. Originally Posted by vart
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. 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