In this tutorial, we will discuss how to convert a decimal number to binary and back to a decimal. This can easily be done by preexisting Java functionalities. But for the sake of curiosity, let us implement our own algorithm first, and then let us discuss how to do with existing Java functions.

### Algorithm to convert a decimal number to binary

First, if you are not familiar with binary representations of numbers, then please perform a quick search about it. There are a log of resources on the internet which can describe it properly. Now, let us discuss the algorithm to convert a decimal to a binary number:

- If the divisible number is still divisible by two, then add a new digit to the binary number,
- The new digit is
**1**, if the division remainder is 1, and**0**if the division remainder is 0. - Go to the first step with the divisible number being the result of the second step.
- Repeat the steps above until the divisible number is 0.
- The reverse of the resulting number is the binary representation.

For example, let us convert the number **14**:

- 14 / 2 = 7, with 0 remainder. Hence, the first digit is
**0** - 7 / 2 = 3, with 1 remainder. Hence, the 2nd digit is
**1** - 3 / 2 = 1, with 1 remainder. Hence, the 3rd digit is
**1** - 1/2 = 0, with 1 remainder. Hence, the 4th digit is
**1 .**Here, we have to stop as the result of the division was 0. - Our final result will be the remainder of the division of the steps above in reverse, hence, the binary representation of 14 will be
**1110.**

Let us implement this algorithm in Java:

1 2 3 4 5 6 7 8 9 10 11 12 | public static String toBinary(int decimalInput){ String result = ""; for(int intermediateNum = decimalInput ; intermediateNum > 0 ; intermediateNum = intermediateNum/2 ){ int bit = intermediateNum%2; result = bit + result; } return result; } |

### Algorithm to convert a binary number to a decimal number

In order to calculate the decimal representation of a binary number, we will need to follow a different algorithm. For each digit of the binary number, we will need to calculate the decimal value, and the sum of all the calculated results will be the final decimal value. Knowing that the value of each digit is 2 to the power of the digit’s position, we can then come up with the following algorithm:

- Starting from the first digit from the left side of the binary number, calculate the decimal value as 2 to the power of the digit’s position – 1.
- Move one digit to the right and repeat the first step.
- Sum all the results of the first step in order to obtain the decimal value for the digits with a value of
**1**. If the digit was a**0 then do not sum the result from the first step.**

For example, to convert the binary number **1110 **to decimal:

- The first digit is a
**1**and has a position of 4. Therefore, we should calculate its value and add it to the total sum. The value will be 2 to the power of 4 -1, which is 2 to the power of 3 =**8**. - The second digit is a
**1**and has a position of 3. Repeating the same process, we calculate 2 to the power of 3 – 1 which is**4** - The third digit is a 1 and has a position of 2. Repeating the same process, we calculate 2 to the power of 2 – 1 which is
**2** - The 4th digit is a 0 and has a position of 2. Repeating the same process, we calculate 2 to the power 1 – 1 which is 1. However, since the digit is a 0, its value is irrelevant and will not be added to the final sum. (You can just skip the 0s and not calculate their 2 to the power of x -1 )
- The decimal value will then be
**8 + 4 + 2**=**14**

Now, let us implement this in Java:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public static int toDecimal(String binaryInput){ int result = 0; for(String intermediateBinaryNum = binaryInput; intermediateBinaryNum.length() > 0 ; intermediateBinaryNum = intermediateBinaryNum.substring(1)){ int currentBit = Integer.parseInt(intermediateBinaryNum.charAt(0) + ""); //append an empty string to convert the char to a string if(currentBit == 1) { result = result + calcToPower(2, intermediateBinaryNum.length()-1); } } return result; } public static int calcToPower(int x, int y){ int result = 1; for(int i = 0; i < y; i++){ result = result * x; } return result; } |

Now, let us try to test our program using a main method:

1 2 3 4 5 6 7 8 9 10 11 | public static void main(String[] args) { int input = 14; String binary = toBinary(input); System.out.println(binary); int decimal = toDecimal(binary); System.out.println(decimal); } |

And the output will be:

1 2 3 4 | 1110 14 Process finished with exit code 0 |

### Using preexisting Java libraries

As mentioned before, binary numbers can be converted from and to using preexisting Java functions. In order to convert a number into its binary representation, we will use the Integer **toBinaryString **function:

1 2 3 | int input2 = 33; String binary2 = Integer.toBinaryString(input2); System.out.println(binary2); |

In order to convert that binary number back to decimal, we can use the Integer **parseInt** function. The function takes two parameters. First is the string representation of the number (which is a binary number in this case), and the 2nd parameter is the base of the number. Since the base of a binary number is 2, we call the function as follows:

1 2 | int fromBinary = Integer.parseInt(binary2, 2); System.out.println(fromBinary); |

Putting it all together, we would get the following output:

1 2 3 4 | 100001 33 Process finished with exit code 0 |

If you like this content, then please support us by clicking that twitter follow button at the bottom of the page in order to be notified of our latest updates 😉