Search code examples
javanumericaldata-conversion

Converting a number from decimal numeral system to all other systems (from 2 to 16) in Java


Is there any easy way to do that without creating my own methods? I only find this:

static int parseInt(String s, int radix)

Which is told to be good only for 2, 10, 8 and 16. What about the rest?


Solution

  • parseInt is for converting from a string representation of a number with a particular radix, to an integer value. If you want to convert a number to a representation in a particular radix, use

    Integer.toString(n, radix);
    

    where radix can be anything from 2 to 36. After that we run out of letters.