Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I read in hex values from a binary file and decipher some bytes containing bitflag values?
    text
    copied!<p>I'm reading a bunch of bytes from a binary file. It is a RAR file. I'm interested in the 11th and 12th bytes of the file because the header specifications state:</p> <blockquote> <p>HEAD_FLAGS Bit flags: 2 bytes</p> <pre><code> 0x0001 - Volume attribute (archive volume) 0x0002 - Archive comment present RAR 3.x uses the separate comment block and does not set this flag. 0x0004 - Archive lock attribute 0x0008 - Solid attribute (solid archive) 0x0010 - New volume naming scheme ('volname.partN.rar') 0x0020 - Authenticity information present RAR 3.x does not set this flag. 0x0040 - Recovery record present 0x0080 - Block headers are encrypted 0x0100 - First volume (set only by RAR 3.0 and later) other bits in HEAD_FLAGS are reserved for internal use </code></pre> </blockquote> <p>The file that I'm playing with has <code>00</code> and <code>0D</code> as positions 11 and 12 respectively. </p> <p>I can't make sense of these two values as they are bit flags (which I have failed to understand).</p> <p>I have these two values in <code>byte</code> array that is 12 bytes long. What I need to check in this sequence is whether the flag <code>0x0100</code> and <code>0x0001</code> is set or not.</p> <p>I'm lost with this. Thanks.</p> <hr> <p>I've inspected some of the files in a Hex editor and what i've seen is that 11th and 12th bytes need to be read together. That's why the specs list all the bit flags are 4 letter hex codes. Checking the bit flags individually yields incorrect results.</p> <hr> <p>Assimilating as much information from the answers/tips, 've solved this in the following way:</p> <pre><code>FileInputStream fisFileInputStream = new FileInputStream((new File("C:\\testarchive.r00")); byte[] bytHeader = new byte[20]; //The first 20 bytes are the RAR header. fisFileInputStream.read(bytHeader); short val=(short)( ((bytHeader[10]&amp;0xFF)&lt;&lt;8) | (bytHeader[11]&amp;0xFF) ); //Joining the two bytes into a short System.out.println("Volume Attribute (0x0001): " + ((val &amp; 0x0001) != 0)); System.out.println("First volume (0x0100): " + ((val &amp; 0x0100) != 0)); </code></pre> <p>I've tried this code with multiple RAR archives — spanned ones, non-spanned ones, the first file of a spanned archive, another file of a spanned archive.</p> <p>The code itself works fine except for a very minor quirk. I get the opposite results from my hex values i.e. </p> <p>When inspecting a file which <strong>is not the first file</strong> in a spanned archive, I get the volume atrribute <code>0x0001</code> as not set (<code>false</code>), and the "First volume" <code>0x100</code> as set (<code>true</code>). </p> <p>When inspecting a file which <strong>is the first file</strong> in a spanned archive, I get the exact opposite results.</p> <p>Now I modify my code to believe that the original specs are wrong (highly unlikely) and that <code>0x0001</code> means that is is the first file in a spanned archive and <code>0x0100</code> means that is a spanned archive, then it's all okay.</p> <p>..but I guess I'm doing something wrong here with my bit flag logic. Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload