![]() |
Binary and decimal are two fundamental number systems used in computing. While humans typically interact with decimal numbers (0-9), computers internally store and process data using binary digits (0s and 1s). Converting between these systems is often necessary when working with computer programs or manipulating data. This article explores how to write a shell script that converts a binary number to its decimal equivalent. Understanding the Conversion ProcessConverting a binary number to decimal involves multiplying each binary digit by its corresponding power of 2, starting from the rightmost digit as the exponent and increasing by 1 for each subsequent digit to the left. Finally, we sum the products of these individual multiplications to obtain the decimal equivalent. Given a binary number as input, we need to make a conversion from a binary number to its equivalent decimal number. Examples: Input : 1011
Output : 11
Input : 1111
Output : 15
Binary to decimalThe idea is to retain the decimal number from the binary number, so, take the binary number and traverse from right to left, taking one by one the last digit of the binary number. Pick the last digit and multiply by the proper base (power of two) and add it to the final variable (say num). Repeat this process until the binary number is not equal to zero and then print the result (that is, num). For example, let’s consider the binary number Example 1 : n = 10101 (Binary number)
Summing these values: 1 + 0 + 4 + 0 + 16 = 21 Example 2: n = 101 (Binary number) num = 1*(2^2) + 0*(2^1) + 1*(2^0) = 6 (resultant decimal) Example 3: n = 1101 (Binary number) num =1*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0) = 13 (resultant decimal) Here We will extract the decimal number from the given binary number. Shell Script to Convert Binary to Decimal
Step by Step Explanation of the Shell Script to Convert Binary to Decimal1. Setting up the script:
2. Validating the input:
3. Initializing variables:
4. Converting binary to decimal:
5. Printing the result:
How to execute the bash files?Write the bash code into a text file using text editors. In this example we are using Vim text editor and saving our file with `.sh` extension. Open terminal and execute the file using below command: vim Binary.sh
Making Script Executable chmod +x Binary.sh
Execute the Script ./Binary.sh
Output: ![]() Shell Script to convert binary to decimal ConclusionIn this article we discussed how to write a shell script to convert binary to decimal . To sum it up, this article is about turning computer language (binary) into numbers we humans use (decimal). It explains the process step by step and gives an easy-to-use script (like a computer recipe) using a program called Bash. The article shows why it’s important to understand this conversion, gives examples, and helps people practice it with the provided script. It’s like learning a new language and then using a simple tool to speak it. |
Reffered: https://www.geeksforgeeks.org
Linux Unix |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |