If you start with a line that you know is <= 8 characters long, you can do something like this:
s = "00000000".substring(0, 8 - s.length()) + s;
Actually, this also works:
s = "00000000".substring(s.length()) + s;
If you are not sure that s is no more than 8 characters long, you need to check it before using any of the above (or use Math.min(8, s.length()) or prepare to detect IndexOutOfBoundsException ).
If you start with an integer and want to convert it to hex with the addition, you can do this:
String s = String.format("%08x", Integer.valueOf(val));
source share