i have to convert a string into ascii format and then into it's binary.
I know how to convert between int to binary using toBinaryString, so how to convert ascii to int.
Or is there any other way to convert string characters into binary form.
In short suppose there is a string like "how are you" , so i have to convert it into binary form.

Recommended Answers

All 7 Replies

I don't know whether this is an good way of coding but i tried converting String to binary and i don't think Java uses ASCII it uses UNICODE CODE POINT.
Anyways what you can do is first convert your String to byte array by using the getBytes() method and then store all byte array elements to integer array and then you already know how to convert from int to binary.

Are you asking something like how to convert this String: "A" to this: "1000001"

Are you asking something like how to convert this String: "A" to this: "1000001"

no i want to convert the string for eg. "hello world" into binary formati.e in 1 and 0's form.

Sorry, can you explain what you want that is different than what I asked. Other than my example's String was 1 letter vs 11 in yours.

What do you mean by "binary format"? I assumed you wanted a String of 0s and 1s.

Can you give a short example of what you want?

Sorry, can you explain what you want that is different than what I asked. Other than my example's String was 1 letter vs 11 in yours.

What do you mean by "binary format"? I assumed you wanted a String of 0s and 1s.

Can you give a short example of what you want?

i mean:
string= more than 1 charcters eg."hi what are you doing"
binary format= string of 0 nd 1 eg. 101010011

commented: That is exactly what NormR1 just posted. -3

Then my example shows what you want.
Soni's post gives you a way to do that.

Another way would be to use the String class's methods to get each character and then use the toBinaryString method to create the String. You'd need to pad them to 8 characters.

Then my example shows what you want.
Soni's post gives you a way to do that.

Another way would be to use the String class's methods to get each character and then use the toBinaryString method to create the String. You'd need to pad them to 8 characters.

ok i will try and see if i can get it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.