Results 1 to 17 of 17
Like Tree3Likes
  • 2 Post By Dark
  • 1 Post By Tolls

Thread: How To change String to binary.

  1. #1
    Khan05 is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default How To change String to binary.

    hi Every body, I am new to java. My team lead told me to write some code Change strings to binary layer.Please give me a good example step by step. I will appreciate. I am a Entry level person.

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    It's not clear exactly what you're after - can you give an example?

    What is an example String you might want to change to binary, and what would the result of the change look like?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    Even though you are a entry level person, Let us know what exactly you need.
    Mak
    (Living @ Virtual World)

  5. #5
    Khan05 is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    I mean if you have string " Hello How are you" how do I change this to binary.
    my code is like
    import java.io*;

    public class Stringtobinary{
    Public static void main(Strings[] ars) {
    String s= "Hello"
    System.out.prinln(s.getBytes(s);

    But I cant se result in 0 and 1. I want to print 0 and 1

  6. #6
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    Hi,

    Java Code:
    public class StringToBinary {
    	
    	public static void main(String[] args) {
    		
    		String name="makpandian";
    		
    		byte[] by=name.getBytes();
    		
    		for(byte b:by){
    			
    			System.out.print(Integer.toBinaryString((int)b));
    		}
    		
    		
    	}
    
    }
    Mak
    (Living @ Virtual World)

  7. #7
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    Hi,

    Java Code:
    public class StringToBinary {
    	
    	public static void main(String[] args) {
    		
    		String name="makpandian";
    		
    		byte[] by=name.getBytes();
    		
    		for(byte b:by){
    			
    			System.out.print(Integer.toBinaryString((int)b));
    		}
    		
    		
    	}
    
    }
    Hope the above code is that you wanted.
    Mak
    (Living @ Virtual World)

  8. #8
    Khan05 is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Thanks MakPandian
    I really appreciate your effort. I was bugging since yesterday. I am new in java.

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @makpandian, don't spoon feed. Especially not to people who are still learning the basics. You're not helping them learn by giving them code, they often will take this code and claim it as their own when they turn it in as an assignment.
    Fubarable and Norm like this.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    7

    Default

    Dark,
    I strongly agree with you. This mistake will never ever ever happen again..
    Mak
    (Living @ Virtual World)

  11. #11
    Khan05 is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    @ Dark
    I am not agreed with you.I am new in Java field I don't really like to copy some one else code. But some time you don't understand some thing that's why people ask help from others. If you have knowledge and you share with some one else it will increase not decrease.This is the only reason people join Forums. @ ManPandian don't warry you can help the people they will appreciate.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    @Khan05 What did you learn from ManPandian's post?
    Do you now know how to look thru the API doc for the classes with methods that will do what you want?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by Khan05 View Post
    @ Dark
    I am not agreed with you.I am new in Java field I don't really like to copy some one else code. But some time you don't understand some thing that's why people ask help from others. If you have knowledge and you share with some one else it will increase not decrease.This is the only reason people join Forums. @ ManPandian don't warry you can help the people they will appreciate.
    Sharing knowledge is teaching someone to fish.
    What makpandian did was simply give you a fish.
    Dark likes this.

  14. #14
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Like Toll said, You can give a man a fish and he will eat for a day, teach a man to fish and he will eat for the rest of his life.

    @Khan05, Because it was handed to you I doubt you really even know whats going on in the code. What we would've done, is pushed you in the direction you needed to go in. Guided you towards your destination, forcing you to learn about everything you needed to know to finish your code. By making you figure out your own problems, with guided help, you will find you learn a lot more about it.

    Java Code:
    for(byte b:by){
    Do you know the purpose of this loop? Why it was used instead of a different one? What the loop is actually doing? Why he used a b:by instead of a for(int i=0;i<lengthOfMyArray.length;i++)?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  15. #15
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    @dr.code.skm, Make your own topic with your error message if it doesn't compile. If it does, then show us the expected output and the actual output. Formulate your question in an Short, Self Contained, Correct Example and make sure to be very clear with what the problem is.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  16. #16
    Khan05 is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    @Dark. sorry,
    I really like your fish example. I went through first java API list. I found some example. I did not figure out how to change into binary.But Dark you can help me to explain why he use for( b:by).
    thanks Dark

  17. #17
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I can't claim credit for coming up with it, its been around since before I was even an egg inside of my mother.

    Read these articles about the Enhanced For Loop, it will better describe them than I can.
    Java Tips - The Enhanced For Loop
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. converting binary to string
    By stegano in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 08:20 PM
  2. Best way convert string to binary and back
    By torsion in forum Advanced Java
    Replies: 1
    Last Post: 03-24-2011, 08:03 AM
  3. Convert String to Binary equivalent
    By vinayak.top in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2011, 09:07 AM
  4. Replies: 12
    Last Post: 11-26-2010, 09:33 AM
  5. Convert String to Binary
    By erakhman in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 09:25 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
  •