title: Creating 'mpint' value in Java (The Secure Shell (SSH) Protocol Architecture - RFC 4251)
body: <p>I'm trying to create <code>mpint</code> string using BigInteger as specified in <a href="https://www.ietf.org/rfc/rfc4251.txt" rel="nofollow">RFC4251</a>:</p>
  
  <blockquote>
    <p>mpint</p>
    
    <p>Represents multiple precision integers in two's complement format,
     stored as a string, 8 bits per byte, MSB first.  Negative numbers
     have the value 1 as the most significant bit of the first byte of
     the data partition.  If the most significant bit would be set for
     a positive number, the number MUST be preceded by a zero byte.
     Unnecessary leading bytes with the value 0 or 255 MUST NOT be
     included.  The value zero MUST be stored as a string with zero
     bytes of data.</p>
    
    <p>By convention, a number that is used in modular computations in
     Z_n SHOULD be represented in the range 0 &lt;= x &lt; n.</p>
    
    <p>Examples:</p>
  
  <pre><code>     value (hex)        representation (hex)
       -----------        --------------------
       0                  00 00 00 00
       9a378f9b2e332a7    00 00 00 08 09 a3 78 f9 b2 e3 32 a7
       80                 00 00 00 02 00 80
       -1234              00 00 00 02 ed cc
       -deadbeef          00 00 00 05 ff 21 52 41 11
  </code></pre>
  </blockquote>
  
  <p>Everything almost clear, but how to explain "<em>Unnecessary leading bytes with the value 0 or 255 MUST NOT be included.</em>"?</p>
  
  <p>And the second question is about this line: <em>"By convention, a number that is used in modular computations in Z_n SHOULD be represented in the range 0 &lt;= x &lt; n."</em>. How to explain it?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>My first suggestion is:</p>
  
  <pre><code>/**
   * Write 'mpint' to output stream including length.
   *
   * @param dos output stream
   * @param bi the value to be written
   */
  public static void writeMPInt(DataOutputStream dos, BigInteger bi) throws IOException {
  
      byte[] twos = bi.toByteArray();
  
      dos.writeInt(twos.length);
      dos.write(twos);
  }
  </code></pre>
  
  <p>Does this method is valid according mentioned above rules?</p>