Literally, integer values ββare not converted from one base to another. Thanks to von Neumann, one of the pioneers of computing who thought that the idea of ββusing base 10 arithmetic on a binary scheme does not make sense, integers are stored in binary format, and we are not trying to change that :-) What are we talking about converting them to strings representing them in some base (another base 10, which is the default) and converting strings to integers in bases other than the base (default) 10. The necessary tools are the static methods of the Integer class.
Java provides Integer.toString(int i, int radix)a conversion function that takes an integer and a base (base) and returns that whole string representation in that base.
String hexRepr = Integer.toString(255, 16)
To go the other way around, that is, from a string representing a number in another database, there is Integer.parseInt(String s, int radix)
int myNum = Integer.parseInt("FF", 16)
, radix1, radix2, , :
int radix1 = 16;
int radix2 = 4;
String input = "FF";
String converted = Integer.toString(Integer.parseInt(radix1Representation, radix1),radix2);
API. , , , .
public static String toString(int i, int radix)
radix, .
, Character.MIN_RADIX , Character.MAX_RADIX, 10.
, ASCII '-' ('\u002D'). , .
. , ββ '0' ('\u0030'); . ASCII:
0123456789abcdefghijklmnopqrstuvwxyz
'\u0030' '\u0039' '\u0061' '\u007A'. radix N, N radix-N . , ( 16) 0123456789abcdef. , String.toUpperCase():
Integer.toString(n, 16).toUpperCase()
: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString%28int,%20int%29
public static int parseInt(String s, int radix) throws NumberFormatException
, . ( , Character.digit(char, int) ), , ASCII '-' ('\u002D'), ASCII plus '+' ('\u002B'), . .
NumberFormatException , :
null .
Character.MIN_RADIX Character.MAX_RADIX.
, , '-' ('\u002D') '+' ('\u002B'), 1.
, , int.
:
parseInt("0", 10)
parseInt("473", 10)
parseInt("+42", 10)
parseInt("-0", 10)
parseInt("-FF", 16)
parseInt("1100110", 2)
parseInt("2147483647", 10)
parseInt("-2147483648", 10)
parseInt("2147483648", 10)
parseInt("99", 8)
parseInt("Kona", 10)
parseInt("Kona", 27)
: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String,%20int%29