×

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal:
  • Using toOcatlString() method
  • Using user-defined logic
Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer wrapper class that converts decimal to an octal string. The signature of the method is given below: public static String toOctalString(int decimal) Example In the following example, we have invoked the toOctalString() method of Integer wrapper class. The method parses an integer argument which you want to convert and returns the octal representation of the decimal number.
public class DecimalToOctalExample
{
public static void main(String args[])
{
System.out.println(Integer.toOctalString(21));
System.out.println(Integer.toOctalString(67));
}
}
Output
25
103
Using user-defined logic You can also convert decimal to octal in using user-defined logic. Example In the following example, we have defined a method of name conversion. Inside the method body, we have taken two variables rem (represent remainder) of type integer, and octal (represent octal number) of type String. We have also taken an array octalchar[]of characters, and assign values from '0'to '7' into it. The method has a while loop that checks the condition and performs the iterations. Let’s see how the while loop will be executed. Remember: The second statement of the while loop performs concatenation instead of addition.If you change the sequence of the statement octal=octal+octalchars[rem]then the output will be 41 which is the wrong output. So remember the sequence while you write this statement. For first iteration decimal=12                                                                                         //passed parameter 12>0                                                                                                        //condition true rem=12%8 = 4                                                                                   //finds remainder octal=octalchars[4]+octal = 4                                      //updated value of octal is 4 decimal=12/8 = 1                                                              //updated value of decimal is 1 For second iteration decimal=1 1>0                                                                                                           //conditiontrue rem=1%8 = 1                                                                                      //finds remainder octal=octalchars[1]+octal = 14                                    //updated value of octal is 4 decimal=1/8 = 0                                                                                //updated value of decimal is 0 For third iteration decimal=0 0>0                                                                                                         //condition false The while loop terminates the execution, and returns the converted octal value. Similarly you can also perform iteration for the decimal value 75. Hence the third iteration will not be executed. The while loop terminates the execution. The method conversion returns the octalvalue.
public class DecimalToOctalExample1
{
public static String conversion(int decimal)                           //called method
{
int rem;
String octal="";
char octalchars[]={'0','1','2','3','4','5','6','7'};
while(decimal>0)
{
rem=decimal%8;
octal=octalchars[rem]+octal;
decimal=decimal/8;
}
return octal;
}
public static void main(String args[])
{
System.out.println("Decimal to octal of 12 is: "+conversion(12));                                               //methodcalling
System.out.println("Decimal to octal of 75 is: "+conversion (75));
}
}
Output
Decimal to Octal of 12 is: 14
Decimal to octal of 75 is: 113

Related Topics

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.