converting binary string to array

Hi All,

i am trying to convert binary string like "10101101" to int a[8]. like int a[8]={1,0,1,0,1,1,0,1}; for data proccessing. please help.

BR,

Aliaj00
You do it the same way you would do it using pen and paper.
Why? The char array "10101101" can be treated like an integer array in C and C++. There is little difference between the string literal and an integer array.

Edit: nevermind -- silly answer on my part.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
	size_t MAX_ELEMENTS = 8, index = 0;
	int a[MAX_ELEMENTS];
	char ch;
	while (cin >> ch && index++ < MAX_ELEMENTS){
		a[index - 1] = ch - '0';
		cout << a[index - 1] << endl;
	}
}
Topic archived. No new replies allowed.