In programming, we often have to deal with numbers. Sometimes we would encounter situations where we are required to convert numbers from one numbering system to another numbering system. This tutorial concentrates on converting numbers between a binary numbering system to a decimal numbering system or vice-versa. This tutorial is to demonstrate the mathematical formula for converting between binary and decimal. The method of this article would represent and store binary numbers in strings format.

A decimal number is a base ten numbering system which uses ten symbols to represents values with the lowest value being a 0 and the highest value being a 9. A binary numbering system is a base two numbering system that only uses 1 and 0 to represent values.

### How To Convert Binary To Decimal

The formula for converting a binary digit to a decimal digit is as follow (n*2

^{p}) + (n*2

^{p}) + …. . The breakdown of the formula can be explained in the following steps.

- Starting at the right of the binary string.
- Treat the binary digit zero as if it is a decimal zero, and one as if it is a decimal one.
- Each binary digit is multiplied to two by the power of their position from right to left of the string, and the first position being position zero.
- Sum the total of each digit multiply to two by the power of their position together. The result of this equation is the decimal value of the binary string.

An example of this formula is below.

Example: Converting binary 1111 to decimal Step 1: (1x2e3) + (1x2e2) + (1x2e1) + (1x2e0) Step 2: 8 + 4 + 2 1 Answer: 15

To put the above formula in a programming context, we would require a loop that will read the binary string from the end to the beginning of the string. When we start at the end of the string, our indexer for the loop would be the string’s length, and we are starting at the last position in the string. However, our power base for the multiplication procedure starts at zero. Therefore we would require another indexer to keep track of our power base, and every time we loop through our binary string, we would need to keep track and increase our power base by one. Another note is the power base of any number to zero will equates to multiply by one.

The for loop below demonstrate that we would use the times variable to keep track of our power base. At the end of each loop, we would multiply our count by two which in terms would equate to 2^{times}. The most important requirement for this loop is the binary character at the current position have to multiply to the power base first in the loop before we can increase the power base for the next loop execution.

When binary digits are stored in string format, they have to be converted to number storage format for the computer system to recognize that it is a number. Without the conversion, the computer will treat the binary digit as a string format and will produce an error result or will throw an exception. Therefore the binary digits within our binary string must be converted to a number value before we can apply the mathematical equation on the digits.

for ( int i = invalue.length() - 1 ; i > -1 ; i--){ output = output + (Long.parseLong(""+invalue.charAt(i),10) * times); times = times * 2; }

### How To Convert Decimal To Binary

The formula for converting a decimal digit to a binary digit is as follow:

- Divide the decimal number by two until the decimal number become zero.
- Each time the decimal number is divide, a binary digit would be produced base on two conditions.
- If the decimal number is divisible by two, we produce a zero binary digit.
- If the decimal number is not divisible by two, we would minus one to the decimal number and divide the number by two. We then write down a one as the binary digit value.
- Write down the binary digit from each division and the first digit being the rightmost digit in the answer string.

Example: Convert 254 decimal to binary Equation Binary Digit Binary Digit After Each Equation Step 1 : 254 / 2 = 127 0 0 Step 2 : (127 - 1 ) / 2 = 63 1 1 0 Step 3 : (63 - 1 ) / 2 = 31 1 1 1 0 Step 4 : (31 - 1 ) / 2 = 15 1 1 1 1 0 Step 5 : (15 - 1 ) / 2 = 7 1 1 1 1 1 0 Step 6 : ( 7 - 1 ) / 2 = 3 1 1 1 1 1 1 0 Step 7 : ( 3 - 1 ) / 2 = 1 1 1 1 1 1 1 1 0 Step 8 : ( 1 - 1 ) / 2 = 0 1 1 1 1 1 1 1 1 0 Answer: 254 decimal value is 11111110 in binary value.

To put the above formula into a programming perspective, We can see the procedures are as follows. We need a loop that would execute while the input value is above zero. Every time the loop execute, we would have to keep track of two things. First, we check to see if the input value is divisible by two. If it is divisible by two we add zero as a binary digit to our output string. Second, if the input value is not divisible by two we add a one as a binary digit to our output string.

After any conditional clause executed, we would have to divide our input value by two bases on two conditions. If the number is not divisible by two, we minus one of the input value and divide it by two. If the number is divisible by two, we simply just divide the number by two. Any binary digits that we are adding to our output string must be to the left side of our output string.

This code block below demonstrates the above statements into a practical scripting format for Java. In the event where the input value is zero, we will return a four digit binary string of zero.

while (invalue > 0){ if (invalue % 2 == 0){ invalue = invalue / 2; output = "0" + output; } else if (invalue % 2 != 0){ invalue = (invalue - 1) / 2; output = "1" + output; } } if (invalue == 0){ return "0000"; }

This code block below is how you can compress all the if and else statement into two lines. The principle is still the same. Instead of evaluating the conditions in an if and else if conditional loop, we could evaluate the conditions through variable assignment base on conditions. The output variable will evaluate if the input value is not divisible by two then we assign a one to the output string. If it is divisible by two then we append zero to the front of our output string.

After assigning the output value in the loop, we then modify the input value base on conditions. Otherwise, the loop will execute indefinitely. If the current input value is divisive by two, we would divide the input value by two. Otherwise, we would assign the input value with the value of itself minus one then divide by two.

if (invalue == 0){ return "0000"; } while (invalue > 0){ output = (invalue % 2 != 0)? "1" + output : "0" + output; invalue = (invalue % 2 == 0)? (invalue / 2): (invalue - 1) / 2; }

The source code below are the two functions that were mentioned in this article.

**Advertisement**

// "Copyright Notice", please do not remove. // Written by Kevin Ng // The full tutorial on this subject can be found @ http://kevinhng86.iblog.website or http://programming.world.edu. // This source code file is a part of Kevin Ng's Z library. // This source code is licenses under CCDL-1.0 A copy of CDDL1.0 can be found at https://opensource.org/licenses/CDDL-1.0 // End "Copyright Notice" // Note: Support up to 63 binary digits string on a 64 bits operating environment or a 31 binary digits string on a 32 bits operating environment. public class libZ { public static void main(String[] args) { // Test case String a = "101110111111111111111111110000000000111000000001101010011110010"; long b = bindecZ(a); String c = decbinZ(b); System.out.println(a); System.out.println(b); System.out.println(c); } public static long bindecZ(String invalue){ long times = 1; long output = 0; for ( int i = invalue.length() - 1 ; i > -1 ; i--){ output = output + (Long.parseLong(""+invalue.charAt(i),10) * times); times = times * 2; } return output; } public static String decbinZ(long invalue) { String output = ""; if (invalue == 0){ return "0000"; } while (invalue > 0){ output = (invalue % 2 != 0)? "1" + output : "0" + output; invalue = (invalue % 2 == 0)? (invalue / 2): (invalue - 1) / 2; } // This code block is when the full set of 8 binary digit must be return. //while ( output.length() % 8 != 0){ // output = "0" + output; //} return output; } }

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.

Original Post Name: "Working With Number – Converting Binary And Decimal – Java".

Original Post Link: http://kevinhng86.iblog.website/2017/01/25/working-with-number-converting-binary-and-decimal-java/.

**Advertisement**