📌  相关文章
📜  从任何基数转换为十进制,反之亦然

📅  最后修改于: 2021-04-29 18:47:51             🧑  作者: Mango

给定一个数字及其基数,将其转换为十进制。数字的底数可以是任何数字,以便可以使用0到9和A到Z表示所有数字。A的值为10,B的值为11,依此类推。编写一个将数字转换为十进制的函数。

例子:

Input number is given as string and output is an integer.

Input: str = "1100", base = 2 
Output: 12

Input: str = "11A", base = 16
Output: 282

Input: str = "123",  base = 8
Output: 83 

强烈建议您最小化浏览器,然后自己尝试。
我们始终可以使用以下公式将任何基数转换为十进制。

"str" is input number as a string 
"base" is the base of the input number.

Decimal Equivalent is,
  1*str[len-1] + base*str[len-2] + (base)2*str[len-3] + ...

下面是上述公式的实现。

C
// C program to convert a number from any base
// to decimal
#include 
#include 
 
// To return value of a char. For example, 2 is
// returned for '2'.  10 is returned for 'A', 11
// for 'B'
int val(char c)
{
    if (c >= '0' && c <= '9')
        return (int)c - '0';
    else
        return (int)c - 'A' + 10;
}
 
// Function to convert a number from given base 'b'
// to decimal
int toDeci(char *str, int base)
{
    int len = strlen(str);
    int power = 1; // Initialize power of base
    int num = 0;  // Initialize result
    int i;
 
    // Decimal equivalent is str[len-1]*1 +
    // str[len-2]*base + str[len-3]*(base^2) + ...
    for (i = len - 1; i >= 0; i--)
    {
        // A digit in input number must be
        // less than number's base
        if (val(str[i]) >= base)
        {
           printf("Invalid Number");
           return -1;
        }
 
        num += val(str[i]) * power;
        power = power * base;
    }
 
    return num;
}
 
// Driver code
int main()
{
    char str[] = "11A";
    int base = 16;
    printf("Decimal equivalent of %s in base %d is "
           " %d\n", str, base, toDeci(str, base));
    return 0;
}


Java
// Java program to convert
// a number from any base
// to decimal
import java.io.*;
 
class GFG
{
// To return value of a char.
// For example, 2 is returned
// for '2'. 10 is returned
// for 'A', 11 for 'B'
static int val(char c)
{
    if (c >= '0' && c <= '9')
        return (int)c - '0';
    else
        return (int)c - 'A' + 10;
}
 
// Function to convert a
// number from given base
// 'b' to decimal
static int toDeci(String str,
                  int base)
{
    int len = str.length();
    int power = 1; // Initialize
                   // power of base
    int num = 0; // Initialize result
    int i;
 
    // Decimal equivalent is
    // str[len-1]*1 + str[len-2] *
    // base + str[len-3]*(base^2) + ...
    for (i = len - 1; i >= 0; i--)
    {
        // A digit in input number
        // must be less than
        // number's base
        if (val(str.charAt(i)) >= base)
        {
        System.out.println("Invalid Number");
        return -1;
        }
 
        num += val(str.charAt(i)) * power;
        power = power * base;
    }
 
    return num;
}
 
// Driver code
public static void main (String[] args)
{
    String str = "11A";
    int base = 16;
    System.out.println("Decimal equivalent of "+
                        str + " in base "+ base +
                                     " is "+ " "+
                              toDeci(str, base));
}
}
 
// This code is contributed
// by anuj_67.


Python3
# Python program to convert a
# number from any base to decimal
 
# To return value of a char.
# For example, 2 is returned
# for '2'. 10 is returned for 'A',
# 11 for 'B'
def val(c):
    if c >= '0' and c <= '9':
        return ord(c) - ord('0')
    else:
        return ord(c) - ord('A') + 10;
 
# Function to convert a number
# from given base 'b' to decimal
def toDeci(str,base):
    llen = len(str)
    power = 1 #Initialize power of base
    num = 0     #Initialize result
 
    # Decimal equivalent is str[len-1]*1 +
    # str[len-2]*base + str[len-3]*(base^2) + ...
    for i in range(llen - 1, -1, -1):
         
        # A digit in input number must
        # be less than number's base
        if val(str[i]) >= base:
            print('Invalid Number')
            return -1
        num += val(str[i]) * power
        power = power * base
    return num
     
# Driver code
strr = "11A"
base = 16
print('Decimal equivalent of', strr,
              'in base', base, 'is',
                 toDeci(strr, base))
 
# This code is contributed
# by Sahil shelangia


C#
// C# program to convert
// a number from any base
// to decimal
using System;
 
class GFG
{
// To return value of a char.
// For example, 2 is returned
// for '2'. 10 is returned
// for 'A', 11 for 'B'
static int val(char c)
{
    if (c >= '0' && c <= '9')
        return (int)c - '0';
    else
        return (int)c - 'A' + 10;
}
 
// Function to convert a
// number from given base
// 'b' to decimal
static int toDeci(string str,
                  int b_ase)
{
    int len = str.Length;
    int power = 1; // Initialize
                   // power of base
    int num = 0; // Initialize result
    int i;
 
    // Decimal equivalent is
    // str[len-1]*1 + str[len-2] *
    // base + str[len-3]*(base^2) + ...
    for (i = len - 1; i >= 0; i--)
    {
        // A digit in input number
        // must be less than
        // number's base
        if (val(str[i]) >= b_ase)
        {
        Console.WriteLine("Invalid Number");
        return -1;
        }
 
        num += val(str[i]) * power;
        power = power * b_ase;
    }
 
    return num;
}
 
// Driver code
public static void Main ()
{
    string str = "11A";
    int b_ase = 16;
    Console.WriteLine("Decimal equivalent of "+
                     str + " in base "+ b_ase +
                  " is " + toDeci(str, b_ase));
}
}
 
// This code is contributed
// by anuj_67.


PHP
= '0' && $c <= '9')
        return ord($c) - ord('0');
    else
        return ord($c) - ord('A') + 10;
}
 
// Function to convert a number from given
// base 'b' to decimal
function toDeci($str, $base)
{
    $len = strlen($str);
    $power = 1; // Initialize power of base
    $num = 0; // Initialize result
 
    // Decimal equivalent is str[len-1]*1 +
    // str[len-2]*base + str[len-3]*(base^2) + ...
    for ($i = $len - 1; $i >= 0; $i--)
    {
        // A digit in input number must be
        // less than number's base
        if (val($str[$i]) >= $base)
        {
            print("Invalid Number");
            return -1;
        }
 
        $num += val($str[$i]) * $power;
        $power = $power * $base;
    }
 
    return $num;
}
 
// Driver code
$str = "11A";
$base = 16;
print("Decimal equivalent of $str " .
      "in base $base is " . toDeci($str, $base));
 
// This code is contributed by mits
?>


Javascript


C
// C Program to convert decimal to any given base
#include 
#include 
 
// To return char for a value. For example '2'
// is returned for 2. 'A' is returned for 10. 'B'
// for 11
char reVal(int num)
{
    if (num >= 0 && num <= 9)
        return (char)(num + '0');
    else
        return (char)(num - 10 + 'A');
}
 
// Utility function to reverse a string
void strev(char *str)
{
    int len = strlen(str);
    int i;
    for (i = 0; i < len/2; i++)
    {
        char temp = str[i];
        str[i] = str[len-i-1];
        str[len-i-1] = temp;
    }
}
 
// Function to convert a given decimal number
// to a base 'base' and
char* fromDeci(char res[], int base, int inputNum)
{
    int index = 0;  // Initialize index of result
 
    // Convert input number is given base by repeatedly
    // dividing it by base and taking remainder
    while (inputNum > 0)
    {
        res[index++] = reVal(inputNum % base);
        inputNum /= base;
    }
    res[index] = '\0';
 
    // Reverse the result
    strev(res);
 
    return res;
}
 
// Driver program
int main()
{
    int inputNum = 282, base = 16;
    char res[100];
    printf("Equivalent of %d in base %d is "
           " %s\n", inputNum, base, fromDeci(res, base, inputNum));
    return 0;
}


Java
// Java Program to convert decimal to any given base
import java.lang.*;
import java.io.*;
import java.util.*;
 
class GFG
{
     
// To return char for a value. For
// example '2' is returned for 2.
// 'A' is returned for 10. 'B' for 11
static char reVal(int num)
{
    if (num >= 0 && num <= 9)
        return (char)(num + 48);
    else
        return (char)(num - 10 + 65);
}
 
// Function to convert a given decimal number
// to a base 'base' and
static String fromDeci(int base1, int inputNum)
{
    String s = "";
 
    // Convert input number is given
    // base by repeatedly dividing it
    // by base and taking remainder
    while (inputNum > 0)
    {
        s += reVal(inputNum % base1);
        inputNum /= base1;
    }
    StringBuilder ix = new StringBuilder();
 
        // append a string into StringBuilder input1
        ix.append(s);
 
    // Reverse the result
    return new String(ix.reverse());
}
 
// Driver code
public static void main (String[] args)
{
    int inputNum = 282, base1 = 16;
    System.out.println("Equivalent of " + inputNum +
                            " in base "+base1+" is " +
                            fromDeci(base1, inputNum));
}
}
 
// This code is contributed by mits


Python3
# Python3 Program to convert decimal to
# any given base
 
# To return char for a value. For example
# '2' is returned for 2. 'A' is returned
# for 10. 'B' for 11
def reVal(num):
 
    if (num >= 0 and num <= 9):
        return chr(num + ord('0'));
    else:
        return chr(num - 10 + ord('A'));
 
# Utility function to reverse a string
def strev(str):
 
    len = len(str);
    for i in range(int(len / 2)):
        temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
 
# Function to convert a given decimal
# number to a base 'base' and
def fromDeci(res, base, inputNum):
 
    index = 0; # Initialize index of result
 
    # Convert input number is given base
    # by repeatedly dividing it by base
    # and taking remainder
    while (inputNum > 0):
        res+= reVal(inputNum % base);
        inputNum = int(inputNum / base);
 
    # Reverse the result
    res = res[::-1];
 
    return res;
 
# Driver Code
inputNum = 282;
base = 16;
res = "";
print("Equivalent of", inputNum, "in base",
       base, "is", fromDeci(res, base, inputNum));
 
# This code is contributed by mits


C#
// C# Program to convert decimal to any given base
using System;
using System.Collections;
 
class GFG
{
     
// To return char for a value. For
// example '2' is returned for 2.
// 'A' is returned for 10. 'B' for 11
static char reVal(int num)
{
    if (num >= 0 && num <= 9)
        return (char)(num + 48);
    else
        return (char)(num - 10 + 65);
}
 
// Function to convert a given decimal number
// to a base 'base' and
static string fromDeci(int base1, int inputNum)
{
    string s = "";
 
    // Convert input number is given
    // base by repeatedly dividing it
    // by base and taking remainder
    while (inputNum > 0)
    {
        s += reVal(inputNum % base1);
        inputNum /= base1;
    }
    char[] res = s.ToCharArray();
 
    // Reverse the result
    Array.Reverse(res);
    return new String(res);
}
 
// Driver code
static void Main()
{
    int inputNum = 282, base1 = 16;
    Console.WriteLine("Equivalent of " + inputNum +
                            " in base "+base1+" is " +
                            fromDeci(base1, inputNum));
}
}
 
// This code is contributed by mits


PHP
= 0 && $num <= 9)
        return chr($num + ord('0'));
    else
        return chr($num - 10 + ord('A'));
}
 
// Utility function to reverse a string
function strev($str)
{
    $len = strlen($str);
    for ($i = 0; $i < $len / 2; $i++)
    {
        $temp = $str[$i];
        $str[$i] = $str[$len - $i - 1];
        $str[$len - $i - 1] = $temp;
    }
}
 
// Function to convert a given decimal
// number to a base 'base' and
function fromDeci($res, $base, $inputNum)
{
    $index = 0; // Initialize index of result
 
    // Convert input number is given base
    // by repeatedly dividing it by base
    // and taking remainder
    while ($inputNum > 0)
    {
        $res.= reVal($inputNum % $base);
        $inputNum = (int)($inputNum / $base);
    }
 
    // Reverse the result
    $res = strrev($res);
 
    return $res;
}
 
// Driver Code
$inputNum = 282;
$base = 16;
$res = "";
print("Equivalent of $inputNum in base $base is " .
                 fromDeci($res, $base, $inputNum));
 
// This code is contributed by mits
?>


输出 :

Decimal equivalent of 11A in base 16 is 282

怎么做反向?
假设给定的输入十进制数为“ inputNum”,目标基数为“ base”。我们反复将inputNum除以基数,然后存储余数。最后,我们反转获得的字符串。下面是C的实现。

C

// C Program to convert decimal to any given base
#include 
#include 
 
// To return char for a value. For example '2'
// is returned for 2. 'A' is returned for 10. 'B'
// for 11
char reVal(int num)
{
    if (num >= 0 && num <= 9)
        return (char)(num + '0');
    else
        return (char)(num - 10 + 'A');
}
 
// Utility function to reverse a string
void strev(char *str)
{
    int len = strlen(str);
    int i;
    for (i = 0; i < len/2; i++)
    {
        char temp = str[i];
        str[i] = str[len-i-1];
        str[len-i-1] = temp;
    }
}
 
// Function to convert a given decimal number
// to a base 'base' and
char* fromDeci(char res[], int base, int inputNum)
{
    int index = 0;  // Initialize index of result
 
    // Convert input number is given base by repeatedly
    // dividing it by base and taking remainder
    while (inputNum > 0)
    {
        res[index++] = reVal(inputNum % base);
        inputNum /= base;
    }
    res[index] = '\0';
 
    // Reverse the result
    strev(res);
 
    return res;
}
 
// Driver program
int main()
{
    int inputNum = 282, base = 16;
    char res[100];
    printf("Equivalent of %d in base %d is "
           " %s\n", inputNum, base, fromDeci(res, base, inputNum));
    return 0;
}

Java

// Java Program to convert decimal to any given base
import java.lang.*;
import java.io.*;
import java.util.*;
 
class GFG
{
     
// To return char for a value. For
// example '2' is returned for 2.
// 'A' is returned for 10. 'B' for 11
static char reVal(int num)
{
    if (num >= 0 && num <= 9)
        return (char)(num + 48);
    else
        return (char)(num - 10 + 65);
}
 
// Function to convert a given decimal number
// to a base 'base' and
static String fromDeci(int base1, int inputNum)
{
    String s = "";
 
    // Convert input number is given
    // base by repeatedly dividing it
    // by base and taking remainder
    while (inputNum > 0)
    {
        s += reVal(inputNum % base1);
        inputNum /= base1;
    }
    StringBuilder ix = new StringBuilder();
 
        // append a string into StringBuilder input1
        ix.append(s);
 
    // Reverse the result
    return new String(ix.reverse());
}
 
// Driver code
public static void main (String[] args)
{
    int inputNum = 282, base1 = 16;
    System.out.println("Equivalent of " + inputNum +
                            " in base "+base1+" is " +
                            fromDeci(base1, inputNum));
}
}
 
// This code is contributed by mits

Python3

# Python3 Program to convert decimal to
# any given base
 
# To return char for a value. For example
# '2' is returned for 2. 'A' is returned
# for 10. 'B' for 11
def reVal(num):
 
    if (num >= 0 and num <= 9):
        return chr(num + ord('0'));
    else:
        return chr(num - 10 + ord('A'));
 
# Utility function to reverse a string
def strev(str):
 
    len = len(str);
    for i in range(int(len / 2)):
        temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
 
# Function to convert a given decimal
# number to a base 'base' and
def fromDeci(res, base, inputNum):
 
    index = 0; # Initialize index of result
 
    # Convert input number is given base
    # by repeatedly dividing it by base
    # and taking remainder
    while (inputNum > 0):
        res+= reVal(inputNum % base);
        inputNum = int(inputNum / base);
 
    # Reverse the result
    res = res[::-1];
 
    return res;
 
# Driver Code
inputNum = 282;
base = 16;
res = "";
print("Equivalent of", inputNum, "in base",
       base, "is", fromDeci(res, base, inputNum));
 
# This code is contributed by mits

C#

// C# Program to convert decimal to any given base
using System;
using System.Collections;
 
class GFG
{
     
// To return char for a value. For
// example '2' is returned for 2.
// 'A' is returned for 10. 'B' for 11
static char reVal(int num)
{
    if (num >= 0 && num <= 9)
        return (char)(num + 48);
    else
        return (char)(num - 10 + 65);
}
 
// Function to convert a given decimal number
// to a base 'base' and
static string fromDeci(int base1, int inputNum)
{
    string s = "";
 
    // Convert input number is given
    // base by repeatedly dividing it
    // by base and taking remainder
    while (inputNum > 0)
    {
        s += reVal(inputNum % base1);
        inputNum /= base1;
    }
    char[] res = s.ToCharArray();
 
    // Reverse the result
    Array.Reverse(res);
    return new String(res);
}
 
// Driver code
static void Main()
{
    int inputNum = 282, base1 = 16;
    Console.WriteLine("Equivalent of " + inputNum +
                            " in base "+base1+" is " +
                            fromDeci(base1, inputNum));
}
}
 
// This code is contributed by mits

的PHP

= 0 && $num <= 9)
        return chr($num + ord('0'));
    else
        return chr($num - 10 + ord('A'));
}
 
// Utility function to reverse a string
function strev($str)
{
    $len = strlen($str);
    for ($i = 0; $i < $len / 2; $i++)
    {
        $temp = $str[$i];
        $str[$i] = $str[$len - $i - 1];
        $str[$len - $i - 1] = $temp;
    }
}
 
// Function to convert a given decimal
// number to a base 'base' and
function fromDeci($res, $base, $inputNum)
{
    $index = 0; // Initialize index of result
 
    // Convert input number is given base
    // by repeatedly dividing it by base
    // and taking remainder
    while ($inputNum > 0)
    {
        $res.= reVal($inputNum % $base);
        $inputNum = (int)($inputNum / $base);
    }
 
    // Reverse the result
    $res = strrev($res);
 
    return $res;
}
 
// Driver Code
$inputNum = 282;
$base = 16;
$res = "";
print("Equivalent of $inputNum in base $base is " .
                 fromDeci($res, $base, $inputNum));
 
// This code is contributed by mits
?>

输出 :

Equivalent of 282 in base 16 is  11A