How do you convert a number from one base to another base in Java?

Programming Answer

Click here to type your answer

Safarov answered

First you should keep in mind that Java only stores decimal system, for other systems you will need String type to store in memory.  Here are some declarations of numbers in different systems:

Integer  base10  =  100;  // decimal number

String base2 = "1110" // binary number

String base16 = "FF" //  hexadecimal number

To convert any base to decimal you will need following code:

Integer.parseInt("1110", 2); //  converts any String  ( here "1110" )  from binary to decimal and returns Integer (here 14)

To convert decimal to any base you will need toString() method:

Integer.toString(256, 16); // converts any Integer (here 100) from decimal to hexadecimal  and returns String ( here "FF" ) 

0 points