I know I could use String.substring or write some extra code, but is there a simple way to achieve this by only using String.format?
For example, I only want the first 6 chars "1234ab" in the result:
int v = 0x1234abcd;
String s = String.format("%06x", v) // gives me 1234abcd
String s = String.format("%06.6x", v) // gives me IllegalformatPrecesionException
The Java Formatter doc said the precision could be used to limit the overall output width, but only to certain data types.
Any ideas? Thanks.
Depending how may hex digits you want to truncate...
You can divide by powers of 16
Results:
Even if you mess up and divide by a power of 16 that exceeds the length of your hex string, the result will just be zero.
Then there's the
substring()
approach