Results 1 to 5 of 5
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default Binary and ASCII

    Guys, how can I check if a file I am about to open is binary or ASCII

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    well... ASCII is binary too :D I don't know of a way to auto-detect, since many plain binary sequences do represent real ascii characters, it'd be hard to tell just by looking at the file (with java, not your eyes).

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Now posted at New To Java - Binary or ASCII (Sun forums)

    @OP: It would be good to actually tell the rest of us (and link) when the discussion is going on at other places.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    well... ASCII is binary too :D I don't know of a way to auto-detect, since many plain binary sequences do represent real ascii characters, it'd be hard to tell just by looking at the file (with java, not your eyes).
    Not that it matters much but there are filing systems that keep a 'type' word with every file: text, binary, executable, script etc. Os Z comes to mind but I don't think Java is implemented for those beasts.

    kind regards,

    Jos

  5. #5
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    I encountered this issue, while working with files
    that contained words from foreign alphabets so I used this code:


    Java Code:
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.charset.Charset;
    import java.nio.charset.CharsetDecoder;
    import java.nio.charset.CharacterCodingException;
    
    /**
    	Test if char is ASCII or not
    */
    
    
    public class TestAscii {
    
    	public static void main (String args[])
    	throws Exception {
    		// this String throws an Exception, it contains an accented letter
    		String test = "Réal";
    		// this String is OK
    		//String test = "Real";
    
    		byte bytearray []  = test.getBytes();
    		System.out.println("Test string : " + test);
    
    		CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
    		try {
    			CharBuffer r = d.decode(ByteBuffer.wrap(bytearray));
    			r.toString();
    		}
    		catch(CharacterCodingException e) {
    			System.out.println("only regular ASCII characters please!");
    			// interrupt the processing
    			throw new Exception(e);
    		}
    		System.out.println("Ok, it's ASCII only!");
    	}
    }

Similar Threads

  1. Help with ASCII Drawing--For loops--if/else
    By alice.li.mastriano in forum New To Java
    Replies: 1
    Last Post: 09-13-2009, 12:31 AM
  2. Getting ascii from a String
    By ali_sakar in forum New To Java
    Replies: 1
    Last Post: 03-07-2009, 10:11 AM
  3. Another ascii question....
    By akira220984 in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 01:38 AM
  4. I need help with ascii characters
    By Grandon in forum Eclipse
    Replies: 17
    Last Post: 11-08-2008, 02:12 AM
  5. Ascii code.........
    By Somitesh Chakraborty in forum New To Java
    Replies: 6
    Last Post: 11-04-2008, 05:32 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
  •