title: Java - Convert int to Byte Array of 4 Bytes?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java">Convert integer into byte array (Java)</a>  </p>
  </blockquote>
  
  
  
  <p>I need to store the length of a buffer, in a byte array 4 bytes large.</p>
  
  <p>Pseudo code:</p>
  
  <pre><code>private byte[] convertLengthToByte(byte[] myBuffer)
  {
      int length = myBuffer.length;
  
      byte[] byteLength = new byte[4];
  
      //here is where I need to convert the int length to a byte array
      byteLength = length.toByteArray;
  
      return byteLength;
  }
  </code></pre>
  
  <p>What would be the best way of accomplishing this? Keeping in mind I must convert that byte array back to an integer later.</p>