Results 1 to 12 of 12
  1. #1
    greg677 is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default decode binary to characters

    Hey guys i just need a little help, i have created a java program to read binary from an external file eg, input.txt, and the notation is as follows 00000001 00000100 so all the binary is on one line separated by a space, now my program currently reads the binary values and prints them onto the screen, which is fine, the part which trouble me is decoding the binary to become a character, so the user can find out the "secret message". can anyone provide help with this. Thanks

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    What exactly have you tried? Or are you having problems trying to figure out how to go about this?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    greg677 is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    @ StormyWaters
    Nope i am having problems trying to figure out the notation so the binary can be translated to ASCII

    @ Eranga
    01000001 01000010 01000011 00001010 01000100 01000101 01000110 00001010 00110000 00110001 00110010 00110011

    for example this bit of binary is supposed to display
    ABC
    DEF
    0123

    so its binary to ASCII

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    for example lets use binary "10100011010010" thats is code for character "?".

    first change it from String to Integer:
    int code = Integer.parseInt("10100011010010",2);
    second: cast it from Integer to Char:
    System.out.println((char)code);


    little program:
    Java Code:
    public class binaryToChar {
    	public static void main(String[] args) {
    		int code = Integer.parseInt("10100011010010",2);
    		System.out.println((char)code);
    	}
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by cselic View Post
    for example lets use binary "10100011010010" thats is code for character "?".

    first change it from String to Integer:
    int code = Integer.parseInt("10100011010010",2);
    second: cast it from Integer to Char:
    System.out.println((char)code);


    little program:
    Java Code:
    public class binaryToChar {
    	public static void main(String[] args) {
    		int code = Integer.parseInt("10100011010010",2);
    		System.out.println((char)code);
    	}
    }
    Try following code segments, you found two things.

    Java Code:
            int code = Integer.parseInt("10100011", 2);
            System.out.println((char) code);
    Java Code:
            int code = Integer.parseInt("10100011010010", 2);
            System.out.println((char) code);

  8. #8
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    Try following code segments, you found two things.
    Yes, you're right.
    I fix it.

    Java Code:
    public class binaryToChar {
    	public static void main(String[] args) {
    		int code = Integer.decode("10100011");
    		System.out.println((char)code);
    	}
    }

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    greg677 is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    This solution works

    Java Code:
    public class binaryToChar {
    	public static void main(String[] args) {
    		int code = Integer.parseInt("10100011010010",2);
    		System.out.println((char)code);
    	}
    }
    however how can i get this program to convert binary numbers from a txt document? here an example document
    Attached Files Attached Files

  11. #11
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I suggest you first try to read a file. See if you can successfully read the file and print it out as is, then try to translate it.

    Here's a Tutorial that will help:
    Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yeah, step down your problem first of all and solve them step by step. I think you get enough comments on conversion you want. If you go through them carefully it's possible to find the solution. So give a try from the first step and let us know if you stuck with any. Your effort make a real change lol.

Similar Threads

  1. Decode this piece of Code
    By mikeyl62 in forum New To Java
    Replies: 2
    Last Post: 02-27-2010, 08:59 PM
  2. code and decode?
    By jeffrey in forum New To Java
    Replies: 5
    Last Post: 08-07-2009, 09:18 AM
  3. Getting problem in UTF-8 Encode/Decode with Java
    By sagarsway in forum Advanced Java
    Replies: 2
    Last Post: 12-22-2008, 07:01 PM
  4. allowable characters from URLDecoder.decode(String
    By Nicholas Jordan in forum Networking
    Replies: 4
    Last Post: 10-18-2008, 05:46 PM
  5. MimeUtility.decode encoding
    By mwildam in forum Advanced Java
    Replies: 2
    Last Post: 08-19-2008, 02:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •