Results 1 to 3 of 3
  1. #1
    sigge is offline Member
    Join Date
    Jan 2012
    Rep Power

    Default Reading a binary file


    I have a binary file that I want to read and parse. It uses a special format, and I have a text document that describes the standard.
    Lets say I read the contents of the file into a byte buffer. Now I need to parse it, x number of bits at a time.

    So I need for example first read 7 bits, and assign it to a variable, and then read the next 3 bits and assign to another variable, and then read 26 bits and assign them into a variable.
    The standard specifies the datatypes, so for example the 7 bits i read should be cast to a int and so on.

    Any tips on how i can do this the best way?

    Thanks in advance

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Reading a binary file

    To get at bits you will need to use the AND operator to strip off the unwanted bits and the SHIFT operator to right adjust the bits. For example to get the first 5 bits of a byte:
    theByte & 0XF8
    theByte >> 3

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Reading a binary file

    I'd use a BitWrapper class the wraps an InputStream; it can hand out single bits while buffering single bytes read from the InputStream. An auxilliary method can collect and return multiple bits. It isn't much code.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. reading a from a binary file
    By trishtren in forum New To Java
    Replies: 2
    Last Post: 05-11-2011, 05:49 PM
  2. Reading Binary File using java
    By pnbalaji in forum New To Java
    Replies: 16
    Last Post: 06-11-2010, 02:50 PM
  3. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  4. Reading a Field in a Binary File
    By janakiram.attuluri in forum Advanced Java
    Replies: 2
    Last Post: 01-09-2008, 10:47 AM
  5. reading a binary file with a RAF
    By jkurth in forum Advanced Java
    Replies: 2
    Last Post: 12-20-2007, 07:30 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