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,031
  • 5
  • 17
  • 35

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
  • 155,977
  • 14
  • 207
  • 269
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
  • 11,440
  • 2
  • 53
  • 63