title: Convert short to byte[] in Java
body: <p>How can I convert a <code>short</code> (2 bytes) to a byte array in Java, e.g.</p>
  
  <pre><code>short x = 233;
  byte[] ret = new byte[2];
  
  ...
  </code></pre>
  
  <p>it should be something like this. But not sure.</p>
  
  <pre><code>((0xFF &lt;&lt; 8) &amp; x) &gt;&gt; 0;
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Also you can use:</p>
  
  <pre><code>java.nio.ByteOrder.nativeOrder();
  </code></pre>
  
  <p>To discover to get whether the native bit order is big or small. In addition the following code is taken from <code>java.io.Bits</code> which does:</p>
  
  <ul>
  <li>byte (array/offset) to boolean</li>
  <li>byte array to char</li>
  <li>byte array to short</li>
  <li>byte array to int</li>
  <li>byte array to float</li>
  <li>byte array to long</li>
  <li>byte array to double</li>
  </ul>
  
  <p>And visa versa.</p>