Saturday, November 1, 2014

Convert java pro



Convert Java Integer object to Numeric primitive types

public class IntegerToNumericPrimitiveTypesExample
public static void main(String[] args)
{
Integer intObj = new Integer("10");
byte b = intObj.byteValue();
System.out.println(b);
short s = intObj.shortValue();
System.out.println(s);
int i = intObj.intValue();
System.out.println(i);
float f = intObj.floatValue();
System.out.println(f);
double d = intObj.doubleValue();
System.out.println(d);
   }
}

Convert Java String to Integer object

public class StringToIntegerExample
{
public static void main(String[] args)
{
Integer intObj1 = new Integer("100");
System.out.println(intObj1);
String str = "100";
Integer intObj2 = Integer.valueOf(str);
System.out.println(intObj2);
     }
}

Convert String to int

public class StringToIntExample
{
           public static void main(String[] args)
          {
String str = new String("10"); 
int i = Integer.parseInt(str);
System.out.println(i);
   }
}

Convert binary number to decimal number

public class ConvertBinaryToDecimalNumber
 {
public static void main(String[] args)
 {
String strBinaryNumber = "111000";
int decimalNumber = Integer.parseInt(strBinaryNumber,2);
System.out.println("Binary number converted to decimal number");
System.out.println("Decimal number is : " + decimalNumber);
   }
}

Convert decimal integer to binary number

public class ConvertIntToBinaryExample
{
public static void main(String[] args)
 {
      int i = 56;
           String strBinaryNumber = Integer.toBinaryString(i);
System.out.println("Convert decimal number to binary number example");
System.out.println("Binary value of " + i + " is " + strBinaryNumber);
     }
}

Convert decimal integer to hexadecimal number

public class ConvertIntToHexExample
{
 public static void main(String[] args)
 {
int i = 32;
String strHexNumber = Integer.toHexString(i);
System.out.println("Convert decimal number to hexadecimal number example");
System.out.println("Hexadecimal value of " + i + " is " + strHexNumber);
    }
}

Convert decimal integer to octal number

public class ConvertIntToOctalExample
{
public static void main(String[] args)
{                
int i = 27;                 
String strOctalNumber = Integer.toOctalString(i);    
System.out.println("Convert decimal number to octal number example");
System.out.println("Octal value of " + i + " is " + strOctalNumber);
               }
}

Convert hexadecimal number to decimal number

public class ConvertHexToDecimalExample
{  
public static void main(String[] args)
{
String strHexNumber = "20";
int decimalNumber = Integer.parseInt(strHexNumber, 16);
System.out.println("Hexadecimal number converted to decimal number");
System.out.println("Decimal number is : " + decimalNumber);           
   }
}

No comments:

Post a Comment