Integer.toOctalString() method in Java

toOctalString() method is a static method of the Integer wrapper class that is used to convert the integer into its corresponding octal string. In this post, we are going to look at the toOctalString() method in detail.

  • Method declaration – public static String toOctalString(int i)
  • What does it do? It will take an integer as an argument and then will convert it into an octal string by processing the input as an unsigned integer
  • What does it return? It will return an octal string

Now, what are signed and unsigned integers? Signed integers contain both positive and negative numbers and their range is from [-2147483648 to 2147483647] whereas unsigned integers contain only the non-negative numbers ranging from [0 to 4294967295].

In this post, we will see how we can convert both positive and negative into their corresponding octal strings.

Using toOctalString() method on positive integers

Positive integers can directly be represented by the octal strings. Let’s try to find out the octal string for number 100.

public class Codekru {

	public static void main(String[] args) {

		int i = 100;

		String octalString = Integer.toOctalString(i);

		System.out.println("Octal representation of " + i + " is: " + octalString);

	}
}

Output –

Octal representation of 100 is: 144
Using toOctalString() method on negative integers

Negative numbers cannot be directly converted to their corresponding octal strings. So, what toOctalString() does is, it first adds 232(4294967296) to the negative number and then returns the octal string of the resulting number.

So, let’s try to find the octal representation of the number -100. Now, toOctalString() will do the following

  • Add 232(4294967296) with the number ( -100 + 232 )
  • Returns the octal representation of the resulting number ( 4294967196 )
public class Codekru {

	public static void main(String[] args) {

		int i = -100;

		String octalString = Integer.toOctalString(i);

		System.out.println("Octal representation of " + i + " is: " + octalString);

	}
}

Output –

Octal representation of -100 is: 37777777634

We can say that the octal representation of -100 and 4294967196 is the same and that is “37777777634”.

What if we wanted to retrieve our original number back by using the octal string?

We can easily do that by using the parseUnsignedInt() method where the base or radix would be 8. So, let’s take the octal strings that we have got in the previous two examples and convert them back to their original integer values.

public class Codekru {

	public static void main(String[] args) {

		String octalStringForPositiveNumber = "144";
		String octalStringForNegativeNumber = "37777777634";

		int positiveNumber = Integer.parseUnsignedInt(octalStringForPositiveNumber, 8);
		int negativeNumber = Integer.parseUnsignedInt(octalStringForNegativeNumber, 8);

		System.out.println("Positive Number: " + positiveNumber);
		System.out.println("Negative Number: " + negativeNumber);

	}
}

Output –

Positive Number: 100
Negative Number: -100

Please visit this link if you want to know more about the Integer wrapper class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Related Articles

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *