## How do I convert decimal to octal?

From the code snippet below you will learn how to convert decimal to an octal string and converting back from a string of octal number to decimal. For the first conversion we can utilize the `Integer.toOctalString()` method. This method takes an integer number an will return the string that represent the corresponding octal number.

To convert back from octal string to decimal number we can use the `Integer.parseInt()` method. Which require two parameter, a string that represent an octal number and the radix which is `8` from octal number.

``````package org.kodejava.example.lang;

public class IntegerToOctalExample {
public static void main(String[] args) {
int integer = 1024;
// Converting integer value to octal string representation.
String octal = Integer.toOctalString(integer);
System.out.printf("Octal value of %d is '%s'.\n", integer, octal);

// Now we converting back from octal string to integer
// by calling Integer.parseInt and passing 8 as the radix.
int original = Integer.parseInt(octal, 8);
System.out.printf("Integer value of octal '%s' is %d.%n", octal, original);

// When for formatting purposes we can actually use printf
// or String.format to display an integer value in other
// format (o = octal, h = hexadecimal).
System.out.printf("Octal value of %1\$d is '%1\$o'.\n", integer);
}
}
``````

Here is the result of our program.

``````Octal value of 1024 is '2000'.
Integer value of octal '2000' is 1024.
Octal value of 1024 is '2000'.
``````

## How do I convert decimal to binary?

In this example you will learn how to convert decimal number to binary number. To convert decimal number to binary we can use `Integer.toBinaryString()` method. This method takes a single parameter of integer and return a string that represent the equal binary number.

If you want to convert the other way around, from binary string to decimal, you can use the `Integer.parseInt()` method. This method takes two parameters. First, the string that represents a binary number to be parsed. The second parameter is the radix to be used while parsing, in case for binary number the radix is `2`.

``````package org.kodejava.example.lang;

public class IntegerToBinaryExample {
public static void main(String[] args) {
int integer = 127;
String binary = Integer.toBinaryString(integer);
System.out.println("Binary value of " + integer + " is "
+ binary + ".");

int original = Integer.parseInt(binary, 2);
System.out.println("Integer value of binary '" + binary
+ "' is " + original + ".");
}

}
``````

Here is the result of our program.

``````Binary value of 127 is 1111111.
Integer value of binary '1111111' is 127.
``````

## How do I convert decimal to hexadecimal?

To convert decimal number (base 10) to hexadecimal number (base 16) we can use the `Integer.toHexString()` method. This method takes an integer number as parameter and return a string that represent the number in hexadecimal.

To convert back the number from hexadecimal to decimal number we can use the `Integer.parseInt()` method. This method takes two argument, the number to be converted, which is the string that represent a hexadecimal number. The second argument is the radix, we pass `16` which tell the method if the string is a hexadecimal number.

``````package org.kodejava.example.lang;