Results 1 to 4 of 4

Thread: binary to text

  1. #1
    stegano is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default 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);
    }
    
    }
    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...

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

    Default

    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.

  3. #3
    stegano is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    dlorde--> if possible,can you show me example of how to do this?

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

    Default

    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

  1. conversion text-binary-text
    By stegano in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 07:14 PM
  2. update binary file from text file
    By billdef in forum New To Java
    Replies: 8
    Last Post: 09-02-2010, 09:24 AM
  3. Converting a text file int binary
    By sruthi_2009 in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 03:09 PM
  4. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  5. Binary text
    By bizmut in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-24-2008, 02:50 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
  •