| by Arround The Web | No comments

How to Convert Binary Numbers into Decimals in C

Binary numbers are combinations of 0s and 1s, whereas decimal numbers are base 10 numbers. In C programming, we convert binary numbers to decimal numbers to improve code performance and make it easier to understand for learners.

This guideline will go over how to convert binary numbers to decimal numbers in C.

Before we get into the concept itself, let’s see what are binary and decimal numbers in C.

Binary and Decimal Number Formats in C

Binary numbers are numbers represented in the form of a combination of two digits 0 and 1, and they are referred to as the base 2 numeral system. Decimal numbers, on the other hand, are base 10 numbers that consist of digits ranging from 0 to 9.

As you see by the name binary which means two so when we have numbers in the form of a combination of two digits 0 and 1, we call them binary numbers. it is referred to as the base 2 numeral system.

Why Convert Binary Numbers into Decimals in C

Working with binary numbers is challenging for developers because of the many possible combinations of 0s and 1s. Decimal numbers, on the other hand, are easier to understand and process, making them a faster and more efficient method for C programs. Converting binary numbers to decimal numbers in C involves multiplying all binary digits by the appropriate power of two and adding the results, which is done using loops.

A Simple Algorithm to Convert Binary to Decimal in C

Here is a simple algorithm to convert binary to decimal in C using six simple steps:

  • Take a binary number as input from the user.
  • Initialize a variable ‘decimal’ to 0 and a variable ‘base’ to 1.
  • Extract the rightmost digit of the binary number using the modulus operator (%), and add the product of this digit and the base to the decimal variable.
  • Multiply the base variable by 2.
  • Remove the rightmost digit from the binary number using integer division (/).
  • Repeat steps 3-5 until all digits have been processed.
  • Display the decimal value of the binary number stored in the ‘decimal’ variable.

Convert Binary Numbers to Decimals in C

The following is a simple code to convert binary numbers to decimals in C programming.

#include <stdio.h>

#include <math.h>

int main() {

  long long a;

printf("Please Insert a Binary number: ");

scanf("%lld", &a);

printf("%lld in binary = %d in the form of decimal", a, binaryToDecimal(a));

  return 0;}

  int binaryToDecimal(long long a) {

  int dec = 0, b = 0, r;

 while (a!=0) {

  r = a % 10;

  a /= 10;

  dec += r * pow(2, b);

  ++b;

 }

 return dec;

}

In the above code, a global function is made as the name “binaryToDecimal”. Then in the main, we declare a long long variable as “a” and ask the user to add a binary number and convert it into a decimal by calling the “binaryToDecimal” function with parameter of a”. In the “binaryToDecimal” function definition a decimal conversion through the while loop.

Output

You can also use a built-in strtol() function to convert binary numbers into decimals in C programming.

The following is the code for such a function:

#include <stdio.h>

#include <stdlib.h>

int main() {

  char binary_string[] = "1110";

  char *ptr;

  long decimal_value;

decimal_value = strtol(binary_string, &ptr, 2);

printf("Binary string "%s" is equal to decimal value %ld.\n", binary_string, decimal_value);

  return 0;

}

The above code converts a binary string “1110” into its equivalent decimal value using the strtol() function, which takes the binary string, a pointer to a char pointer, and the base of the number system as arguments. Finally, it prints the result to the console using printf().

Output

Conclusion

As we know humans are familiar with decimal numbers as compared to binary as they are tough to manage. Decimal digits are easy to perform arithmetic operations as they are in base 10 and their processing is faster as compared to binary digits hence, the binary numbers are converted into decimals. The above overview has explained the conversion of binary numbers into decimals with the C program with user-defined functions and built-in strtol() function.

Share Button

Source: linuxhint.com

Leave a Reply