does %d in String.format() work for unsigned integers also?
java
Solution
If you want to treat an int as if it were unsigned you can to
int i = ...
String s = String.format("%d", i & 0xFFFFFFFFL);
This effectively turns the signed int into a long, but it will be from 0 .. 2^31-1
To do the reverse you can do
int i = (int) Long.parseLong(s);
String s2 = String.format("%d", i & 0xFFFFFFFFL);
And `s2` will be the same as `s` provided it is in range.
BTW: The simplest thing to do might be to use a `long` in the first place. Unless you are creating a lot of these the extra memory is trivial and the code is simpler.
Problem
in `printf()` I remember for unsigned there is `%u`... but I can find no such `%u` in specs for `String.format()` so if I have a large `unsigned` int then `%d` will work correctly on it?