You do not need to use String at all.
Try this instead
public static long asBase3(int num) { long ret = 0, factor = 1; while (num > 0) { ret += num % 3 * factor; num /= 3; factor *= 10; } return ret; }
Note: the numbers on the computer are only N-bit, that is 32-bit or 64-bit, that is, they are binary. However, what you can do is create a number that when printed in base 10 will actually appear in base 3.
Peter Lawrey
source share