# binary _ array

• 12-02-2010, 07:44 PM
Engineer
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
????????
• 12-02-2010, 08:12 PM
tashimoto
• 12-02-2010, 08:23 PM
Engineer
Quote:

Originally Posted by tashimoto

thanks:)
i can make an array, but the problem in how can make the binary number in array ???????????????????????
• 12-02-2010, 08:27 PM
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.
• 12-02-2010, 08:37 PM
Engineer
Quote:

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
Code:

```  try{     System.out.println("Enter your multiplier");     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??
• 12-02-2010, 08:49 PM
Mr.Beans
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.
• 12-02-2010, 08:50 PM
pbrockway2
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.
• 12-02-2010, 08:56 PM
Engineer
sorry,but i cant understand
please,can any one put acode to right understand
thanks
• 12-02-2010, 09:02 PM
Mr.Beans
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.
• 12-03-2010, 06:28 AM
Vinod Mukundan
Quote:

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