Java - Long toString(long i, long radix) method



Description

The Java Long toString(long i, long 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.Long.toString() method

public static String toString(long i, long radix)

Parameters

  • i − This is an long 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

Example 1

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

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

Output

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

Number = 170
toString = 170

Example 2

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

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

Output

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

Number = 170
toString = 252

Example 3

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

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

Output

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

Number = 170
toString = aa

Example 4

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

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

Output

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

Number = 170
toString = 10101010
java_lang_long.htm
Advertisements