Coding Villa

Convert String into Integer/Octal/ Decimal/ hex/ byte and short in Java

Author : Jal Pari

Share with Friends
Views : 1318
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this article you will learn how to convert Parse/Convert String into Integer, Octal, and Decimal, hexadecimal, byte and short in Java.


Advertisements


 

 

Conversion of String into integer:

 

While converting a string into integer you have to pass a string as argument, an exception will occur that will disturb the flow of instructions. Pass the value of String then it will be converted into integer.

 

Here is Sample Code:

 

public class Main {

  public static void main(String[] args) throws Exception {

    String s = "58";

    int i = Integer.valueOf(s).intValue();

    // or

    i = Integer.parseInt(s);

  }

}

 

 

 

Conversion of String to octal:

 

Octal: Octal stands for “Eight”

 

While Converting String to Octal you will make a class with public access then you will pass a string as argument, an exception will occur in some cases. You will declare an integer and use the toString method. toString() method reads object and converts it into a string form and returns a string object.

  

Here is simple code that will help you to understand:

 

 

public class Main {

  public static void main(String[] args) throws Exception {

    int i = Integer.parseInt("1000", 8);

    String z = Integer.toString(i, 8);

    System.out.println(z);

  }

}

 

 

Conversion of String to Decimal:

 

To convert String to decimal you will make a class then pass an argument as String, exception occurs. Declare an integer type and pass the value in decimal. parseInt() is a method of  Integer class which converts String to integer

 

Here is sample code to convert String to decimal:

 

 

public class Main {

  public static void main(String[] args) throws Exception {

    int a = Integer.parseInt("1042");

    String b = Integer.toString(a);

    System.out.println(b);

  }

}

 

 

Conversion of String to hexadecimal:

 

Hexadecimal: Hexadecimal number system has 16 characters.

 

To convert String to hexadecimal after making class with public access you will pass String as argument .declare an integer and pass the value of hexa number i.e.16 .toString method is also used. parseInt() is a method of  Integer class which converts String to integer            `


 

public class Main {

  public static void main(String[] args) throws Exception {

    int i = Integer.parseInt("3df", 16);

    String h = Integer.toString(i, 16);

    System.out.println (h);

  }

}

 

 

Conversion of String to Byte:

 

 To convert String to Byte you will make a class. Pass a String as argument. parseByte() is a method of  Byte class which converts String to Byte.

 

Here is sample code that converts String to Byte:

 

 

public class Main {

  public static void main(String[] args) throws Exception {

    byte a = Byte.parseByte("456");

    System.out.println(a);

  }

}

 

 

Conversion of String to Short:

 

To convert String into Short, pass a String as argument. parseShort method is used to convert String to Short.  


 

public class Main {
  public static void main(String[] argv) throws Exception {
    short a = Short.parseShort("678");
    System.out.println(a);
  }
}

 

 

This is simple article to Parse/Convert String into Integer, Octal, and Decimal, hexadecimal, byte and short in Java

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook