# Converting Numbers to Different Bases in Java

on

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
*
*
* @param number :: Integer number to convert.
* @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 ) {
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 ) );

}
}``````
The following two tabs change content below. #### Edward

Edward J. Beckett is a passionate software engineer, web developer, server administrator and polyglot programmer with nearly a decade experience building desktop and web applications ranging from simple personal web sites to enterprise level applications on many technology stacks including Java, Java EE, Spring, Spring MVC, Spring Data, Hibernate, SQL, JPA, JMS, HTML, CSS, JavaScript, ColdFusion, PHP, Node.js and more... #### Latest posts by Edward (see all)

• http://bluemix2.blogspot.com blueMix

nice tutorial,
and ‘neat’ code 