I use java.lang.String.format(String, Object...) to pad an integer with zeros on the left like this:
String.format("%03d", mynumber);
for zero-padding with a length of 3. For hexadecimal output replace the d with an x as in "%03x".
To read more about this format click here.