Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Converting String to Binary

807598Apr 27 2006 — edited May 8 2006
Hi Friends,
Can someone please tell me how to convert a String to a binary number
(4 digits) and then store it in a char array.
For example,if String s = 4, I need to convert them to
"0100" and then store them in a char array.
I tried this code,but I am first converting the string to integer,
and then casting it back to String,and moreover it's 3-digits and not
4.Please help
public class test{
 public static void main(String[] ar){
 	String s = "4";
 	int num  = Integer.parseInt(s);
 	s = Integer.toBinaryString(num);
 	System.out.println("Binary String:"+s);

 	char[] arr = s.toCharArray();
 	for(int i=0;i<arr.length;i++)
 		System.out.println("Char digit:"+arr);

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 5 2006
Added on Apr 27 2006
10 comments
1,095 views