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

    Default Reading a binary file

    Hello,

    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
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    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 online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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
  •