tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Core > Integer > Decimal to Base Conversion

Decimal to Base Conversion 

The following example shows how to convert a number in Decimal system to any other base system and vice versa. Here we considered a system with base 32.

File Name  :  
com/bethecoder/tutorials/core/integer/Integer2OtherBase.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.core.integer;

public class Integer2OtherBase {

  /**
   @param args
   */
  public static void main(String[] args) {

    int num = 121;
    int radix = 32;  
    //where radix >=2   (Character.MIN_RADIX)  && 
    //      radix <= 36 (Character.MAX_RADIX)
    
    String str = Integer.toString(num, radix);
    System.out.println(num + "(decimal) in base32 : " + str);
    
    num = Integer.parseInt(str, radix);
    System.out.println(str + "(base32) in decimal : " + num);
    
    //3p = 3*32^1 + p  * 32^0
    //3p = 3*32   + 25 * 1
    //3p = 96     + 25 * 1
    //3p = 121
  }

}
   

It gives the following output,
121(decimal) in base32 : 3p
3p(base32) in decimal : 121


                    Print

   



Books Recommendation

 
 
bl  br