title: Implementation of i2osp and os2ip
body: <p>I have a work to do in university in I have to code the RSA following the RSA-OAEP specification. My professor give me one pdf with the RFC 3447 specification.</p>
  
  <p>In the pdf I have the following:</p>
  
  <p>I2OSP converts a nonnegative integer to an octet string of a
     specified length.</p>
  
  <p>I2OSP (x, xLen)</p>
  
  <p>Input:
     x        nonnegative integer to be converted
     xLen     intended length of the resulting octet string</p>
  
  <p>Output:
     X        corresponding octet string of length xLen</p>
  
  <p>Error: "integer too large"</p>
  
  <p>Steps:</p>
  
  <ol>
  <li><p>If x >= 256^xLen, output "integer too large" and stop.</p></li>
  <li><p>Write the integer x in its unique xLen-digit representation in
    base 256:</p>
  
  <pre><code> x = x_(xLen-1) 256^(xLen-1) + x_(xLen-2) 256^(xLen-2) + ...
   + x_1 256 + x_0,
  </code></pre>
  
  <p>where 0 &lt;= x_i &lt; 256 (note that one or more leading digits will be
    zero if x is less than 256^(xLen-1)).</p></li>
  <li><p>Let the octet X_i have the integer value x_(xLen-i) for 1 &lt;= i &lt;=
    xLen.  Output the octet string</p>
  
  <pre><code> X = X_1 X_2 ... X_xLen.
  </code></pre></li>
  </ol>
  
  <p>4.2 OS2IP</p>
  
  <p>OS2IP converts an octet string to a nonnegative integer.</p>
  
  <p>OS2IP (X)</p>
  
  <p>Input:
     X        octet string to be converted</p>
  
  <p>Output:
     x        corresponding nonnegative integer</p>
  
  <p>Steps:</p>
  
  <ol>
  <li><p>Let X_1 X_2 ... X_xLen be the octets of X from first to last,
    and let x_(xLen-i) be the integer value of the octet X_i for
    1 &lt;= i &lt;= xLen.</p></li>
  <li><p>Let x = x_(xLen-1) 256^(xLen-1) + x_(xLen-2) 256^(xLen-2) + ...</p>
  
  <ul>
  <li>x_1 256 + x_0.</li>
  </ul></li>
  <li><p>Output x.</p></li>
  </ol>
  
  <p>But I can't understand this pseudo-code, can someone give me one explanation how I do this? The answer can be either in pseudo-code or python implementation.</p>