Monday, 26 August 2013

Trying to not Omit Leading 0's when writing from Int to Hex using buffered writer

Trying to not Omit Leading 0's when writing from Int to Hex using buffered
writer

I create buffered writer
BufferedWriter errorReport = new BufferedWriter(new
FileWriter("ErrorReport.txt"));
Then I wanted to write while converting my integer to a hex.
errorReport.write(Integer.toHexString(version))
This works perfectly, except it omits leading 0's as it writes the minimum
possible length. Say 'version' is a byte in length and simply prints 6.
Well I know the actual value is actually 06. How do I keep these leading
0's?
I tried errorReport.write(String.format("%03x",
Integer.toHexString(version)), but get an error for
illegalFormatConversionException x != java.lang.String

No comments:

Post a Comment