📜  二进制到十进制转换程序

📅  最后修改于: 2021-04-23 07:59:03             🧑  作者: Mango

给定一个二进制数作为输入,我们需要编写一个程序将给定的二进制数转换为等效的十进制数。

例子 :

Input : 111
Output : 7

Input : 1010
Output : 10

Input: 100001
Output: 33

这个想法是从最右边的数字开始提取给定二进制数字的数字,并保留变量dec_value。从二进制数中提取数字时,将数字乘以适当的基数(2的幂),然后将其添加到变量dec_value中。最后,变量dec_value将存储所需的十进制数字。
例如:
如果二进制数是111。
dec_value = 1 *(2 ^ 2)+ 1 *(2 ^ 1)+ 1 *(2 ^ 0)= 7

下图说明了如何将(1010)转换为等效的十进制值:

二进制2十进制

下面是上述想法的实现:

C++
// C++ program to convert binary to decimal
#include 
using namespace std;
 
// Function to convert binary to decimal
int binaryToDecimal(int n)
{
    int num = n;
    int dec_value = 0;
 
    // Initializing base value to 1, i.e 2^0
    int base = 1;
 
    int temp = num;
    while (temp) {
        int last_digit = temp % 10;
        temp = temp / 10;
 
        dec_value += last_digit * base;
 
        base = base * 2;
    }
 
    return dec_value;
}
 
// Driver program to test above function
int main()
{
    int num = 10101001;
 
    cout << binaryToDecimal(num) << endl;
}


Java
// Java program to convert
// binary to decimal
 
// Function to convert
// binary to decimal
class GFG {
    static int binaryToDecimal(int n)
    {
        int num = n;
        int dec_value = 0;
 
        // Initializing base
        // value to 1, i.e 2^0
        int base = 1;
 
        int temp = num;
        while (temp > 0) {
            int last_digit = temp % 10;
            temp = temp / 10;
 
            dec_value += last_digit * base;
 
            base = base * 2;
        }
 
        return dec_value;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int num = 10101001;
        System.out.println(binaryToDecimal(num));
    }
}
 
// This code is contributed by mits.


Python3
# Python3 program to convert
# binary to decimal
 
# Function to convert
# binary to decimal
def binaryToDecimal(n):
    num = n;
    dec_value = 0;
     
    # Initializing base
    # value to 1, i.e 2 ^ 0
    base = 1;
     
    temp = num;
    while(temp):
        last_digit = temp % 10;
        temp = int(temp / 10);
         
        dec_value += last_digit * base;
        base = base * 2;
    return dec_value;
 
# Driver Code
num = 10101001;
print(binaryToDecimal(num));
 
# This code is contributed by mits


C#
// C# program to convert
// binary to decimal
 
// Function to convert
// binary to decimal
class GFG {
    public static int binaryToDecimal(int n)
    {
        int num = n;
        int dec_value = 0;
 
        // Initializing base1
        // value to 1, i.e 2^0
        int base1 = 1;
 
        int temp = num;
        while (temp > 0) {
            int last_digit = temp % 10;
            temp = temp / 10;
 
            dec_value += last_digit * base1;
 
            base1 = base1 * 2;
        }
 
        return dec_value;
    }
 
    // Driver Code
    public static void Main()
    {
        int num = 10101001;
 
        System.Console.Write(binaryToDecimal(num));
    }
}
 
// This code is contributed by mits.


PHP


Javascript


C++
// C++ program to convert binary to decimal
// when input is represented as binary string.
#include 
#include 
using namespace std;
 
// Function to convert binary to decimal
int binaryToDecimal(string n)
{
    string num = n;
    int dec_value = 0;
 
    // Initializing base value to 1, i.e 2^0
    int base = 1;
 
    int len = num.length();
    for (int i = len - 1; i >= 0; i--) {
        if (num[i] == '1')
            dec_value += base;
        base = base * 2;
    }
 
    return dec_value;
}
 
// Driver program to test above function
int main()
{
    string num = "10101001";
    cout << binaryToDecimal(num) << endl;
}


Java
// Java program to convert binary to
// decimal when input is represented
// as binary string.
import java.io.*;
 
class GFG {
 
    // Function to convert binary to decimal
    static int binaryToDecimal(String n)
    {
        String num = n;
        int dec_value = 0;
 
        // Initializing base value to 1,
        // i.e 2^0
        int base = 1;
 
        int len = num.length();
        for (int i = len - 1; i >= 0; i--) {
            if (num.charAt(i) == '1')
                dec_value += base;
            base = base * 2;
        }
 
        return dec_value;
    }
 
    // Driver program to test above function
    public static void main(String[] args)
    {
        String num = new String("10101001");
        System.out.println(binaryToDecimal(num));
    }
}
 
// This code is contribute by Prerna Saini


Python3
# Python3 program to convert binary
# to decimal when input is
# represented as binary string.
 
# Function to convert
# binary to decimal
def binaryToDecimal(n):
    num = n;
    dec_value = 0;
     
    # Initializing base
    # value to 1, i.e 2 ^ 0
    base1 = 1;
     
    len1 = len(num);
    for i in range(len1 - 1, -1, -1):
        if (num[i] == '1'):    
            dec_value += base1;
        base1 = base1 * 2;
     
    return dec_value;
 
# Driver Code
num = "10101001";
print(binaryToDecimal(num));
 
# This code is contributed by mits


C#
// C# program to convert binary to
// decimal when input is represented
// as binary string.
using System;
 
class GFG {
 
    // Function to convert binary to decimal
    static int binaryToDecimal(String n)
    {
        String num = n;
        int dec_value = 0;
 
        // Initializing base value to 1,
        // i.e 2^0
        int base1 = 1;
 
        int len = num.Length;
        for (int i = len - 1; i >= 0; i--) {
            if (num[i] == '1')
                dec_value += base1;
            base1 = base1 * 2;
        }
 
        return dec_value;
    }
 
    // Driver Code
    public static void Main()
    {
        String num = "10101001";
        Console.WriteLine(binaryToDecimal(num));
    }
}
 
// This code is contribute by mits


PHP
= 0; $i--)
    {
        if ($num[$i] == '1')    
            $dec_value += $base;
        $base = $base * 2;
    }
     
    return $dec_value;
}
 
// Driver Code
$num = "10101001";
echo binaryToDecimal($num), "\n";
 
// This code is contributed by aj_36
?>


Javascript


C++
#include 
using namespace std;
 
int main()
{
    char binaryNumber[] = "1001";
     
    cout << stoi(binaryNumber, 0, 2);
 
    return 0;
}
// This code is contributed by whysodarkbro


C
#include 
#include 
#include 
 
int main()
{
    char binaryNumber[] = "1001";
    int bin, dec = 0;
     
    bin = atoi(binaryNumber);
 
    for(int i = 0; bin; i++, bin /= 10)
        if (bin % 10)
            dec += pow(2, i);
 
    printf("%d", dec);
 
    return 0;
}
 
// This code is contributed by whysodarkbro


Java
public class GFG {
    public static void main(String args[])
    {
        String binaryNumber = "1001";
        System.out.println(Integer.parseInt(binaryNumber, 2));
    }
}
 
// This code is contributed
// By Yash Kumar Arora


Python3
n = input()
 
# Convert n to base 2
s = int(n, 2)
 
print(s)


C#
using System;
 
class GFG {
     
public static void Main()
{
    int value = 1001;
    Console.Write(Convert.ToInt32(value.ToString(), 2));
}
}
 
// This code is contributed by SoumikMondal


Javascript


输出:

169

注意:该程序仅适用于整数范围内的二进制数。如果要使用长二进制数(例如20位或30位),则可以使用字符串变量来存储二进制数。

下面是一个类似的程序,它使用字符串变量而不是整数来存储二进制值:

C++

// C++ program to convert binary to decimal
// when input is represented as binary string.
#include 
#include 
using namespace std;
 
// Function to convert binary to decimal
int binaryToDecimal(string n)
{
    string num = n;
    int dec_value = 0;
 
    // Initializing base value to 1, i.e 2^0
    int base = 1;
 
    int len = num.length();
    for (int i = len - 1; i >= 0; i--) {
        if (num[i] == '1')
            dec_value += base;
        base = base * 2;
    }
 
    return dec_value;
}
 
// Driver program to test above function
int main()
{
    string num = "10101001";
    cout << binaryToDecimal(num) << endl;
}

Java

// Java program to convert binary to
// decimal when input is represented
// as binary string.
import java.io.*;
 
class GFG {
 
    // Function to convert binary to decimal
    static int binaryToDecimal(String n)
    {
        String num = n;
        int dec_value = 0;
 
        // Initializing base value to 1,
        // i.e 2^0
        int base = 1;
 
        int len = num.length();
        for (int i = len - 1; i >= 0; i--) {
            if (num.charAt(i) == '1')
                dec_value += base;
            base = base * 2;
        }
 
        return dec_value;
    }
 
    // Driver program to test above function
    public static void main(String[] args)
    {
        String num = new String("10101001");
        System.out.println(binaryToDecimal(num));
    }
}
 
// This code is contribute by Prerna Saini

Python3

# Python3 program to convert binary
# to decimal when input is
# represented as binary string.
 
# Function to convert
# binary to decimal
def binaryToDecimal(n):
    num = n;
    dec_value = 0;
     
    # Initializing base
    # value to 1, i.e 2 ^ 0
    base1 = 1;
     
    len1 = len(num);
    for i in range(len1 - 1, -1, -1):
        if (num[i] == '1'):    
            dec_value += base1;
        base1 = base1 * 2;
     
    return dec_value;
 
# Driver Code
num = "10101001";
print(binaryToDecimal(num));
 
# This code is contributed by mits

C#

// C# program to convert binary to
// decimal when input is represented
// as binary string.
using System;
 
class GFG {
 
    // Function to convert binary to decimal
    static int binaryToDecimal(String n)
    {
        String num = n;
        int dec_value = 0;
 
        // Initializing base value to 1,
        // i.e 2^0
        int base1 = 1;
 
        int len = num.Length;
        for (int i = len - 1; i >= 0; i--) {
            if (num[i] == '1')
                dec_value += base1;
            base1 = base1 * 2;
        }
 
        return dec_value;
    }
 
    // Driver Code
    public static void Main()
    {
        String num = "10101001";
        Console.WriteLine(binaryToDecimal(num));
    }
}
 
// This code is contribute by mits

的PHP

= 0; $i--)
    {
        if ($num[$i] == '1')    
            $dec_value += $base;
        $base = $base * 2;
    }
     
    return $dec_value;
}
 
// Driver Code
$num = "10101001";
echo binaryToDecimal($num), "\n";
 
// This code is contributed by aj_36
?>

Java脚本


输出 :

169

使用预定义的函数:

C++

#include 
using namespace std;
 
int main()
{
    char binaryNumber[] = "1001";
     
    cout << stoi(binaryNumber, 0, 2);
 
    return 0;
}
// This code is contributed by whysodarkbro

C

#include 
#include 
#include 
 
int main()
{
    char binaryNumber[] = "1001";
    int bin, dec = 0;
     
    bin = atoi(binaryNumber);
 
    for(int i = 0; bin; i++, bin /= 10)
        if (bin % 10)
            dec += pow(2, i);
 
    printf("%d", dec);
 
    return 0;
}
 
// This code is contributed by whysodarkbro

Java

public class GFG {
    public static void main(String args[])
    {
        String binaryNumber = "1001";
        System.out.println(Integer.parseInt(binaryNumber, 2));
    }
}
 
// This code is contributed
// By Yash Kumar Arora

Python3

n = input()
 
# Convert n to base 2
s = int(n, 2)
 
print(s)

C#

using System;
 
class GFG {
     
public static void Main()
{
    int value = 1001;
    Console.Write(Convert.ToInt32(value.ToString(), 2));
}
}
 
// This code is contributed by SoumikMondal

Java脚本


输出 :

9