Results 1 to 4 of 4
Thread: binary to text
- 04-12-2011, 03:31 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
binary to text
Hi,this is my code for text to binary conversion.i have problem to convert it back to text.anyone please help me to convert binary to text...XML Code:import java.util.*; public class textToBinary{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("enter text"); String text = input.nextLine();// take input //converting process byte[] bytes = text.getBytes(); StringBuilder binary = new StringBuilder(); for (byte b : bytes) { int val = b; for (int i = 0; i < 8; i++) { binary.append((val & 128)== 0 ? 0 : 1); val <<= 1; } }System.out.println(binary); } }
- 04-12-2011, 04:01 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Split the binary String into chunks of 8 characters.
Use the Integer class to parse each chunk as binary (radix 2) into an int.
Convert the int into character.
Append the character to your output String.
- 04-12-2011, 04:29 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 28
- Rep Power
- 0
dlorde--> if possible,can you show me example of how to do this?
- 04-13-2011, 04:53 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Show me your attempt, and I will help you get it right.
Here are some hints:
You can split the binary String into 8 character chunks using the String.substring method.
You can parse an 8 character String of binary digits into an int using the Integer.parseInt method with a radix of 2.
You can cast the int into a char.
You can append the char to your output String using the += operator.
Similar Threads
-
conversion text-binary-text
By stegano in forum New To JavaReplies: 2Last Post: 03-27-2011, 07:14 PM -
update binary file from text file
By billdef in forum New To JavaReplies: 8Last Post: 09-02-2010, 09:24 AM -
Converting a text file int binary
By sruthi_2009 in forum New To JavaReplies: 0Last Post: 03-23-2009, 03:09 PM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM -
Binary text
By bizmut in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-24-2008, 02:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks