title: How to send such complicated hex, binary protocol data accurately using java byte[]?
body: <p>I am very confused with this kind of stuffs, What should I send as final command, get always confused 8 bits to 1 byte but how do we make it? Is it only the command packet [hex] as shown in screen shot? or Is it header + command packet[hex] shown in two screen shots?</p>
  
  <p><strong>Confusion details:</strong></p>
  
  <ul>
  <li><p>In such diagram header block shows mostly like "Bit 7, Bit 6,..,Bit 0" instead of "Bit 0, Bit 1, Bit 2, ... Bit 7", i always wondering why?. </p></li>
  <li><p>But when I apply in code what is following order for byte st[0] = Bit 7 or Bit 1? </p></li>
  <li><p>Also according to this diagram, does it mean every command I send will have Header fixed always?</p></li>
  <li><p>This is the code I was trying by taking Bit 1 as st[0], Bit 2 as st<a href="https://i.stack.imgur.com/877kv.png" rel="nofollow noreferrer">1</a> instead of Bit 7 as st[0]. To apply Power off/on test.</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  public class test 
  {
  
  public static void main(String[] args) throws UnknownHostException, IOException 
  { 
  
      byte st[]=new byte[256];
      st[0]=0x01; // get    
      st[1]=0x02; // header 2 byte
      st[2]=0x02; // header length            
      st[3]=0;    // command byte
      st[4]=0;    // reserved
      st[5]=0;            
      st[6]=0;             
      st[7]=0;
      st[8]=0x01; // power off
      st[9]=0x30; 
      st[10]=0x01;
      st[11]=0x00; 
      System.out.println(st); // Should this work, am i correct based on diagram?
  
  
      Socket s           = new Socket("192.168.1.2", 49137);
      DataInputStream input         = new DataInputStream(s.getInputStream());          
      DataOutputStream outToServer  = new DataOutputStream(s.getOutputStream());
      BufferedReader i   = new BufferedReader(new InputStreamReader(s.getInputStream()));
      outToServer.write(st);
      String get;
      get = i.readLine();
      System.out.println("FROM SERVER: " + get);
      s.close();        
  }
  </code></pre>
  
  <p>}</p></li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/877kv.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/eh5Cn.png" alt="enter image description here"></p>
  
  <p>P.S: How would you do this really? Every hex command make hand by hand, this PDF file has almost 100 commands, it would take lot of time. Or you manage them in a different way?</p>