Knowledge Walls
John Peter
Pune, Maharashtra, India
How to use Integer.parseInt(String,Radix) in JAVA
26924 Views
Integer.parseInt(String,radix) method is used to parse integer from binary, octal or hexa decimal numbers. It is basically used to convert binary to integer, octal to integer and hexadecimal to integer.

String is alphanumeric value;
Radix is used to specify type of input String.

Value range of radix is 2 to 36. Get radix range from Character.MIN_RADIX and Character.MAX_RADIX.
   2 - base-2 numbers (Binary). 2 means input value only contains 0-1. Otherwise will through NumberFormatException
   8 - base-8 numbers (Octal). 8 means input string contains only 0-7. Otherwise will through NumberFormatException
 10 - base-10 numbers (Decimal). It may contains 0-9 characters.
 16 - base-16 numbers (Hexa decimal). It may contains 0-9 and A,B,C,D,E,F characters.
 36 - base-36 numbers (Numbers and Letters). It may contains 0-9 (10) numbers and A to Z (26) letters.

 13 - It means string can contains 0-9 (10) and A,B and C.
   Integer.parseInt("0293BC",13) - No errors. Return parsed integer value.
  Integer.parseInt("0293DC",13) - through NumberFormatException because it contains D. but range 13 only support 0-9 and A, B and C characters.

Example program for Integer.parseInt(String,Radix)
Class SampleCoder {
    public static void main(String args[]) {
          //Binary to decimal conversion
          System.out.println(Integer.parseInt("101010",2));

          //Octal to decimal conversion
          System.out.println(Integer.parseInt("463",8));

          //Hexadecimal to decimal conversion
          System.out.println(Integer.parseInt("4AC",16));

          //String to decimal conversion - throughs NumberFormat Exception, Because it contains D
          System.out.println(Integer.parseInt("6BDC",13));
    }
}
Next Topics
Next lessons of current book.
Computer software engineer articles of One day One Thing to Know
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details