Java - Integer toString(int i, int radix) method



Description

The Java Integer toString(int i, int radix) method returns a string representation of the first argument i in the radix specified by the second argument radix. If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the radix 10 is used instead.

The following ASCII characters are used as digits: 0123456789abcdefghijklmnopqrstuvwxyz

Declaration

Following is the declaration for java.lang.Integer.toString() method

public static String toString(int i, int radix)

Parameters

  • i − This is an integer to be converted.

  • radix − This is the radix to use in the string representation.

Return Value

This method returns a string representation of the argument in the specified radix.

Exception

NA

Getting String Representation of a Positive int Value in Decimal Format Example

The following example shows the usage of Integer toString(int i, int radix) method to get the decimal string representation of the specified int value. We've created a int variable and assigned it a positive int value. Then using toString() method, we're printing the result.

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 170;
      System.out.println("Number = " + i);
    
      /* returns the decimal string representation of the given number */
      System.out.println("toString = " + Integer.toString(i,10));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 170
toString = 170

Getting String Representation of a Positive int Value in Octal Format Example

The following example shows the usage of Integer toString(int i, int radix) method to get the octal string representation of the specified int value. We've created a int variable and assigned it a positive int value. Then using toString() method, we're printing the result.

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 170;
      System.out.println("Number = " + i);
    
      /* returns the octal string representation of the given number */
      System.out.println("toString = " + Integer.toString(i,8));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 170
toString = 252

Getting String Representation of a Positive int Value in HexaDecimal Format Example

The following example shows the usage of Integer toString(int i, int radix) method to get the hexadecimal string representation of the specified int value. We've created a int variable and assigned it a positive int value. Then using toString() method, we're printing the result.

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 170;
      System.out.println("Number = " + i);
    
      /* returns the hexadecimal string representation of the given number */
      System.out.println("toString = " + Integer.toString(i,16));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 170
toString = aa

Getting String Representation of a Positive int Value in Binary Format Example

The following example shows the usage of Integer toString(int i, int radix) method to get the binary string representation of the specified int value. We've created a int variable and assigned it a positive int value. Then using toString() method, we're printing the result.

package com.tutorialspoint;
public class IntegerDemo {
   public static void main(String[] args) {
      int i = 170;
      System.out.println("Number = " + i);
    
      /* returns the binary string representation of the given number */
      System.out.println("toString = " + Integer.toString(i,2));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Number = 170
toString = 10101010
java_lang_integer.htm
Advertisements