Binary to Decimal conversion in Java
This tutorial would help in learning the following:
- Conversion of numbers from binary-system (0 and 1) to decimal-system (using 0 to 9) in Java.
- Re-conversion of characters into their respective ASCII codes
- Code for Binary to Decimal conversion in JAVA
The decimal number system uses all the 10 digits (0 to 9) to represent a number while the binary number system 0 and 1. The following algorithm is used to convert a binary-number to a decimal-number:
let the binary number be 111111
- Give proper index to each digit of the binary number, starting with zero and from the rightmost digit
number->111111
index—->543210 - multiply the number present at index ‘i’ with the ith power of 2
index 0 -> 1* 20 = 1
index 1 -> 1* 21 = 2
index 2 -> 1* 22 = 4
index 3 -> 1* 23 = 8
index 4 -> 1* 24 = 16
index 5 -> 1* 25 = 32 - add all the products obtained from step 2, to get the equivalent decimal number
1 + 2 + 4 + 8 + 16 + 32 = 63
(111111)2 ~ (63)10
Each character used in computing is assigned a special ASCII code (decimal-number-system); hence, once a binary code is converted to its equivalent decimal-number code, the latter can be assigned a character as per the ASCII convention.
Note, there can be different encoding systems on different machines.
Java program to convert binary to decimal
//This program follows the convention of giving a fullstop after the code for an individual character
public class BinaryDeconversion
{
String binarycode;
BinaryDeconversion()//default constructor
{
binarycode="1000011.1101111.1100100.1100101.100000.1010011.1110000.1100101.1100101.1100100.1111001.";
}
BinaryDeconversion(String x)//parameterized constructor
{
binarycode=x;
}
public static void main(String args[])
{
BinaryDeconversion ob;
try{//considering the command line argument of the user as the string to be re-converted from binary format
ob=new BinaryDeconversion(args[0]);
}//going with the default string in case of any exceptions with the command line argument
catch(Exception e){ob=new BinaryDeconversion();};
System.out.print("The decoded value of binary code "+ob.binarycode+" is: ");
ob.binaryToString(ob.binarycode);
}
char binary(char array[])
{
int code=0;
int temporary;
int i;
int j=0;
for(i=0;array[i]!='\0';i++);//obtaining the index of the last code-character in array[]
for(--i;i>=0;i--){
temporary = ((int)array[i]) - ((int)('0'));
code+=temporary*(int)(Math.pow(2,j++));//Math.pow() helps in giving the required power of 2
}
return (char)code;//returning the character corresponding to the integer-code whose value is in 'code'
}
void binaryToString(String code)
{
char helper[]=new char[10+1];//assuming that the length of binary code for one character will not exceed 10
int index = 0;
for(int i=0;i<code.length();i++){
if(code.charAt(i)=='.')//printing the value of the code for one character
{
helper[index]='\0';
System.out.print(binary(helper));
index=0;
}
else//stacking up the incomplete code for one character
{
helper[index++]=code.charAt(i);
}
}
}
}//class endsOutput (without command line argument):
The decoded value of binary code 1000011.1101111.1100100.1100101.100000.1010011.1110000.1100101.1100101.1100100.1111001. is: Code Speedy
Summary:
char binary(char[]):
- the index of the last character in array[] is stored in ‘i’
- the value of ‘j’ corresponds with the required power of 2 that is required for the character at given index ‘i’
- the numerical value of the character-code at index ‘i’ is stored in ‘temporary’
- the sum of products for different indices is stored in ‘code’
- the character corresponding to the value of ‘code’ is returned
void binaryToString(String)
- helper[] stores the binary code for one character at a time
- binary(char[]) is called and helper[] is passed to it to print the corresponding character for the binary code in helper[]
Also read:
Leave a Reply