code: public static int encodeNum( final long num, final byte[] data, final int beginIndex, final int maxBytes ) {
          for ( int i = 0; i < maxBytes; i++ ) {
              final int pos = maxBytes - i - 1; // the position of the byte in the number
              final int idx = beginIndex + pos; // the index in the data array
              data[idx] = (byte) ( ( num >> ( 8 * i ) ) & 0xff );
          }
          return beginIndex + maxBytes;
      }
docs: Convert a number to bytes (with length of maxBytes) and write bytes into
  the provided byte[] data starting at the specified beginIndex.
  
  @param num
  the number to encode
  @param data
  the byte array into that the number is encoded
  @param beginIndex
  the beginning index of data where to start encoding,
  inclusive.
  @param maxBytes
  the number of bytes to store for the number
  @return the next beginIndex (<code>beginIndex + maxBytes</code>).