Binary to Decimal conversion in C++
This tutorial would help in learning the following:
- Conversion of numbers from binary to decimal in C++.
- Re-conversion of characters into their respective ASCII codes
- Code for Binary to Decimal conversion in C++
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.
C++ program to convert binary to decimal
//Note, this program assumes the convention of giving a fullstop after the octal-code for an individual character #include <stdio.h> #include <math.h>//this contains function pow which will be used to calculate the powers of 2 #define binarystring "1000011.1101111.1100100.1100101.100000.1010011.1110000.1100101.1100101.1100100.1111001." #define max 10//assuming that the length of individual code for a character will not exceed 10 char binary(char array[]) { char code=0;//the sum of products, as explained in the algorithm, would be stored in this variable char c; 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--){ c=array[i]; temporary = c - '0'; code+=temporary*pow(2,j++); } return code;//since the integer sum is returned in a character variable, the corresponding character for the integer code would be assumed } void binaryToString(char array[]) { char helper[max+1];//extra length to accommodate the null character int index = 0; for(int i=0;array[i]!='\0';i++){ if(array[i]=='.')//printing the value of the code for one character { helper[index]='\0'; printf("%c",binary(helper)); index=0; } else//stacking up the incomplete code for one character { helper[index++]=array[i]; } } } int main() { printf("\nThe original form of the binary code %s is: ",binarystring); binaryToString(binarystring); return 0; }
Output:
The original form of the 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’
- value of ‘code’ is returned
void binaryToString(char[])
- 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