Results 1 to 17 of 17
Thread: How To change String to binary.
- 07-19-2011, 12:53 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
- 07-19-2011, 01:07 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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?
-
Also, please feel free to show the fruits of your efforts first. This is not a code factory.
- 07-19-2011, 01:40 PM #4
Even though you are a entry level person, Let us know what exactly you need.
Mak
(Living @ Virtual World)
- 07-19-2011, 02:11 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
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
- 07-19-2011, 02:22 PM #6
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)
- 07-19-2011, 02:23 PM #7
Hi,
Hope the above code is that you wanted.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)
- 07-19-2011, 04:11 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
Thanks MakPandian
I really appreciate your effort. I was bugging since yesterday. I am new in java.
- 07-19-2011, 04:13 PM #9
@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.
- 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.
- 07-20-2011, 06:58 AM #10
Dark,
I strongly agree with you. This mistake will never ever ever happen again..Mak
(Living @ Virtual World)
- 07-20-2011, 02:23 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
@ 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.
- 07-20-2011, 02:56 PM #12
@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?
- 07-20-2011, 03:12 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 07-20-2011, 03:47 PM #14
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.
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++)?Java Code:for(byte b:by){- 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.
- 07-20-2011, 03:51 PM #15
@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.
- 07-20-2011, 04:08 PM #16
Member
- Join Date
- Jul 2011
- Posts
- 27
- Rep Power
- 0
@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
- 07-20-2011, 04:21 PM #17
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
-
converting binary to string
By stegano in forum New To JavaReplies: 5Last Post: 03-27-2011, 07:20 PM -
Best way convert string to binary and back
By torsion in forum Advanced JavaReplies: 1Last Post: 03-24-2011, 07:03 AM -
Convert String to Binary equivalent
By vinayak.top in forum Advanced JavaReplies: 1Last Post: 01-21-2011, 08:07 AM -
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Convert String to Binary
By erakhman in forum New To JavaReplies: 1Last Post: 09-01-2010, 08:25 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks