1

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

john stamos
  • 1,054
  • 5
  • 17
  • 36

2 Answers2

1

The x specifies hexadecimal so format will perform the conversion by passing the integer directly

errorReport.write(String.format("%03x", version)); 
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

You're very close. The JVM is complaining that you are trying to hex-format a string. Try errorReport.write(String.format("%03x", version))

lreeder
  • 12,047
  • 2
  • 56
  • 65