I recently saw a question on StackOverflow on how to convert a decimal number into a different base radix in Java. Java natively handles this using Integer.parseInt(String, int). Internally, this operation works by first doing some input validation and then converts each character (Unicode code point) of the string’s value to the desired radix. The conversion happens on a character by character basis… which, is quite interesting and takes a bit of understanding of the internals of the Java Character class. However, the mathematics behind the conversion are pretty easy.

To convert to a decimal number to a new base, recursively return the remainder of the number divided by the radix for each operation until the dividend is zero. Then return the concatenated values in reverse.

Here’s a visual of the mathematics to convert the decimal number 267 to Octal (Base 8) and Hexadecimal (Base 16) . Notice how the remainder of each operation is stored as first in, last out (FILO) for the answer.

After generating the pseudo-code to implement the conversion it’s pretty easy to build a static utility method to convert a number to a new base. For example, below I’m simply converting the decimal value 9658 to it’s base binary value. This works in reverse too… converting a binary value to a different base. However, you need to use the correct literal notation for each representation.

Note, for representations above base ten you’ll have to convert the alpha-chars to their numeric values. I.E. 11-A, 12-B, etc…

```
public class NumberUtil {
/**
* This example is convoluted as in reality it just uses 'toString' to convert the number...
* However, it displays the logic needed to make the conversion...
*
* To convert a number to a new radix, recursively return the remainder of the number
* divided by the radix for each operation until zero. Then return the concatenated value in reverse.
*
* Example convert 9658 to base 2
*
* 9658 / 2 = 4829 R 0
* 4829 / 2 = 2414 R 1
* 2414 / 2 = 1207 R 0
* 1207 / 2 = 603 R 1
* 603 / 2 = 301 R 1
* 301 / 2 = 150 R 1
* 150 / 2 = 75 R 0
* 75 / 2 = 37 R 1
* 37 / 2 = 18 R 1
* 18 / 2 = 9 R 0
* 9 / 2 = 4 R 1
* 4 / 2 = 2 R 0
* 2 / 2 = 1 R 0
* 1 / 2 = 0 R 1
*
* Answer :: 10010110111010
*
* @param number :: Integer number to convert.
* @param radix :: Radix to convert to.
* @return :: BigInteger of the number converted to the desired radix.
*/
static BigInteger convertBase( int number, int radix ) {
List<Integer> remainder = new ArrayList<>();
int count = 0;
String result = "";
while( number != 0 ) {
remainder.add( count, number % radix != 0 ? number % radix : 0 );
number /= radix;
try {
result += remainder.get( count );
} catch( NumberFormatException e ) {
e.printStackTrace();
}
}
return new BigInteger( new StringBuffer( result ).reverse().toString() );
}
public static void main( String[] args ) {
System.out.println( convertBase( 9658, 2 ) );
}
}
```

#### Edward

#### Latest posts by Edward (see all)

- Java Swing Model View Adapter Mediator - September 14, 2015
- Tomcat 8 on Java 8 Behind Apache on CentOS - April 16, 2015
- Getting a Primitive Data Types Wrapper Class in Java - November 13, 2014