1. Member
Join Date
Aug 2010
Location
Egypt
Posts
34
Rep Power
0

## binary _ array

Hello,:)
if i have binary number
ex
0011
how can i make this number in array
[0]=1
[1]=1
[2]=0
[3]=0
????????

2. Member
Join Date
Sep 2010
Location
Oregon, usa
Posts
69
Rep Power
0

3. Member
Join Date
Aug 2010
Location
Egypt
Posts
34
Rep Power
0
Originally Posted by tashimoto
thanks:)
i can make an array, but the problem in how can make the binary number in array ???????????????????????

4. Well, this depends. How are you getting the binary number? Are you going to be passed 1's and 0's via a string. Are you going to be passed an integer? Can the integer be signed or unsigned?

I can't give you an unambiguous response if you give me an ambiguous question.

5. Member
Join Date
Aug 2010
Location
Egypt
Posts
34
Rep Power
0
Originally Posted by Mr.Beans
Well, this depends. How are you getting the binary number? Are you going to be passed 1's and 0's via a string. Are you going to be passed an integer? Can the integer be signed or unsigned?

I can't give you an unambiguous response if you give me an ambiguous question.
ok
PHP Code:
```  try{
multiplier=sc.nextInt();

StringBuilder sb = new StringBuilder();
bin = Integer.toBinaryString(multiplier);
for (int i = bin.length(); i < limit; i++) {
sb.append("0");
}
sb.append(bin);
System.out.println(sb.toString());

}
catch(Exception e){

System.out.println("Enter number *_^");

}```
this my code i want the program to take the binary number and but every element in array
Ex:
0101
i want to take the first element(1) .............
to take it we should put it in array to make address for it as [0]=1, right??

6. That did not help much. However, I see that you are using the toBinaryString method from Integer. Since you have a string of all the binary digits, you simply have to split them up and put them in the array.
This also brings about the question of whether you want an array of integers, (0 or 1) or characters ('0' or '1').

Either way, you can get each individual character in the string using the charAt(int index) method. You can create a for loop which goes through and gets each character in the string, then you can handle each character how you wish.

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
Use a for loop. The StringBuilder method charAt() will give you the character at each position and all you need to do is look at it and decide whether to put a 0 or a 1 into the array.

Note that int arrays are initialised with zeros in them so you might be able to avoid padding the bin string with zero digits.

8. Member
Join Date
Aug 2010
Location
Egypt
Posts
34
Rep Power
0
sorry,but i cant understand
please,can any one put acode to right understand
thanks

9. Java Code:
```String s = Integer.toBinaryString(x);

for(int i = 0; i < s.length(); i++) {
char digit = s.charAt(i);
// Do what you want with digit here
}```
That is the basics of what you want to do. You have to find out the specifics for yourself, as it is your problem.

10. Originally Posted by Engineer
Hello,:)
if i have binary number
ex
0011
how can i make this number in array
[0]=1
[1]=1
[2]=0
[3]=0
????????
char[] ch = bin.toCharArray();

you can use it as you want .....

The best possible way is as described above by using a for loop ....

if you want the reverse of the string and then to array .... then use a StringBuilder and its reverse function and then to char Array...

Lets be more specific so that guys in here can help u out :)

warm regards
Vinod M
Last edited by Vinod Mukundan; 12-03-2010 at 05:32 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•