Tuesday, 6 December 2016

Number Conversion Operation in Java

class MyIntegerToBinary
{

 public static void main(String a[])
 {
 
  // Integer to Binary
  Integer i = new Integer(16);
  String binary = Integer.toBinaryString(i);
  System.out.println("Binary value: "+binary);
 
  // Integer to Hexadeciaml
  String hex = Integer.toHexString(i);
  System.out.println("Hex value: "+hex);
 
   // Integer to Octal
  String octal = Integer.toOctalString(i);
  System.out.println("Octal value: "+octal);
 
 
  //Binary to Integer
  String binary1 = "1101001";
  i = Integer.parseInt(binary,2);
  System.out.println("Integer value: "+i);
 
 
  // Hexa to Integer    
  String hex1 = "FF23";
  i = Integer.parseInt(hex1,16);
  System.out.println("Integer value: "+i);
 
  String str = "-144";
   i = Integer.valueOf(str);
  System.out.println("The integer value: "+i);
 
  String number = "768";
   i = Integer.parseInt(number);
  System.out.println("converted integer: "+i);
 

  String octal1 = "7776";
  i = Integer.parseInt(octal1,8);
  System.out.println("Integer value: "+i);
       
   str = "adef";
   i = Integer.parseInt(str,27);         // 27 - is a radix value
  System.out.println("Integer value: "+i);
 
 }
}

Output:



No comments:

Post a Comment