title: Left padding integers (non-decimal format) with zeros in Java
body: <p>The question <a href="https://stackoverflow.com/questions/473282/left-padding-integers-with-zeros-in-java">has been answered for integers printed in decimal format</a>, but I'm looking for an <em>elegant</em> way to do the same with integers in <em>non-decimal</em> format (like binary, octal, hex).</p>
  
  <p>Creation of such Strings is easy:</p>
  
  <pre><code>String intAsString = Integer.toString(12345, 8);
  </code></pre>
  
  <p>would create a String with the octal represenation of the integer value 12345. But how to format it so that the String has like 10 digits, apart from calculating the number of zeros needed and assembling a new String 'by hand'.</p>
  
  <p>A typical use case would be creating binary numbers with a fixed number of bits (like 16, 32, ...) where one would like to have all digits including leading zeros.</p>