title: how to read the given binary struct using java?
body: <p>I'm trying to decode and read binary data sent from a device, I need to extract a decimal number which is inside the binary structure.
  this is the structure detail:</p>
  
  <p>(2) Synchronization message format
  typedef struct</p>
  
  <pre><code>{
      WORD
      SyncHeader;
      WORD 
      SyncID;
      DWORD UnitID;
  } SyncStruct;
  </code></pre>
  
  <ul>
  <li>SyncHeader is always 0xf8fa</li>
  <li>SyncID is a message sequence number</li>
  <li>UnitID is the unit identification number</li>
  </ul>
  
  <p>For example, received message is</p>
  
  <p><code>0xFA 0xF8 0x1B 0x01 0x81 0x60 0x33 0x3C</code></p>
  
  <ul>
  <li>SyncHeader = 0xF8 0xFA</li>
  <li>SyncID = 0x01 0x1B (Decimal = 283)</li>
  <li>UnitID = 0x3C 0x33 0x60 0x81 (Decimal = 1010000001)</li>
  </ul>
  
  <p>now, having the structure well defined, I have a small java code that reads a different structure but implements the same principle:</p>
  
  <pre><code>private String getId(ChannelBuffer buf) {
  
      String id = "";
      for (int i = 0; i &lt; 7; i++) {
          int b = buf.getUnsignedByte(i);
  
          // First digit
          int d1 = (b &amp; 0xf0) &gt;&gt; 4;
          if (d1 == 0xf) break;
          id += d1;
  
          // Second digit
          int d2 = (b &amp; 0x0f);
          if (d2 == 0xf) break;
          id += d2;
      }
  
      return id;
  }
  </code></pre>
  
  <p>What modifications do I have to apply to my small java code in order to read and store in a variable just the SyncID message sequence number which is a decimal value?</p>
  
  <p>Thanks</p>