📜  将八进制转换为十六进制的程序

📅  最后修改于: 2021-04-22 10:30:38             🧑  作者: Mango

给定八进制数,任务是将其转换为十六进制数。

例子:

Input:  47 
Output: 27
Explanation:
Decimal value of 47 is = (7 * 1) + (4 * 8) = 39

Now, convert this number to hexadecimal
39/16 -> quotient = 2, remainder = 7
2/16 ->  quotient = 0, remainder = 2

So, the equivalent hexadecimal number is = 27

Input: 235
Output:  9d

方法:
八进制数字或简称八进制为8的底数,并使用数字0到7。可以通过将连续的二进制数字分组为三组(从右开始)来由二进制数字组成八进制数字。

十六进制数是一个位置数字系统,其基数或基数为16,并使用十六个不同的符号。它可以是字母和数字的组合。它使用从0到9的数字和字母A到F。

转换步骤:
最简单的方法是将八进制数转换为十进制,然后将十进制转换为十六进制形式。

  1. 从下到上在八进制数字旁边写上8的幂(1、8、64、512、4096等)。
  2. 将每个数字乘以其幂。
  3. 汇总答案。这是十进制解。
  4. 用十进制数除以16。
  5. 获取下一次迭代的整数商(如果数字不会被16均分,则将结果四舍五入到最接近的整数)。
  6. 记下余数,应在0到15之间。
  7. 重复从步骤4开始的步骤,直到商等于0。
  8. 从下到上写出所有余数。
  9. 将大于9的余数转换为十六进制字母。这是十六进制的解决方案。

例如,如果给定的八进制数字为5123:

Digit Power Multiplication
5 512 2560
1 64 64
2 8 16
3 1 3

那么十进制数字(2560 + 64 + 16 + 3)是:2643

Division Quotient Remainder
2643/16 165 3
165/16 10 5
10/16 0 10 (a)

最后,十六进制数是:a53

下面是上述方法的实现:

C++
// C++ program to convert Octal
// to Hexadecimal
#include
using namespace std;
 
// Function to convert octal to decimal
int octalToDecimal(int n)
{
    int num = n;
    int dec_value = 0;
     
    // Initializing base value
    // to 1, i.e 8^0
    int base = 1;
   
    int temp = num;
    while (temp)
    {
         
        // Extracting last digit
        int last_digit = temp % 10;
        temp = temp / 10;
         
        // Multiplying last digit with
        // appropriate base value and
        // adding it to dec_value
        dec_value += last_digit * base;
   
        base = base * 8;
    }
    return dec_value;
}
 
// Function to convert decimal
// to hexadecimal
string decToHexa(int n)
{
     
    // char array to store
    // hexadecimal number
    char hexaDeciNum[100];
     
    // counter for hexadecimal
    // number array
    int i = 0;
    while(n != 0)
    {   
         
        // Temporary variable to
        // store remainder
        int temp  = 0;
           
        // Storing remainder in
        // temp variable.
        temp = n % 16;
           
        // Check if temp < 10
        if (temp < 10)
        {
            hexaDeciNum[i] = temp + 48;
            i++;
        }
        else
        {
            hexaDeciNum[i] = temp + 87;
            i++;
        }
        n = n / 16;
    }
     
    string ans = "";
       
    // Printing hexadecimal number array
    // in reverse order
    for(int j = i - 1; j >= 0; j--)
    {
        ans += hexaDeciNum[j];
    }
    return ans;
}
 
// Driver Code
int main()
{
    string hexnum;
    int decnum, octnum;
     
    // Taking 5123 as an example of
    // Octal Number.
    octnum = 5123;
     
    // Convert Octal to Decimal
    decnum = octalToDecimal(octnum);
     
    // Convert Decimal to Hexadecimal
    hexnum = decToHexa(decnum);
     
    cout << "Equivalent Hexadecimal Value = "
         << hexnum << endl;
}
 
// This code is contributed by pratham76


Java
// Java Program to Convert Octal
// to Hexadecimal
 
import java.util.Scanner;
 
public class JavaProgram {
    public static void main(String args[])
    {
        String octnum, hexnum;
        int decnum;
        Scanner scan = new Scanner(System.in);
         
        // Taking 5123 as an example of
        // Octal Number.
        octnum = "5123";
         
        // Convert Octal to Decimal
        decnum = Integer.parseInt(octnum, 8);
         
        // Convert Decimal to Hexadecimal
        hexnum = Integer.toHexString(decnum);
 
        System.out.print("Equivalent Hexadecimal Value = "
                                                + hexnum);
    }
}


Python3
# Python3 program to convert octal
# to hexadecimal
 
# Taking 5123 as an example of
# octal number
octnum = "5123"
 
# Convert octal to decimal
decnum = int(octnum, 8)
 
# Convert decimal to hexadecimal
hexadecimal = hex(decnum).replace("0x", "")
 
# Printing the hexadecimal value
print("Equivalent Hexadecimal Value =", hexadecimal)
 
# This code is contributed by virusbuddah_


C#
// C# Program to Convert Octal
// to Hexadecimal
using System;
class GFG{
     
// Function to convert octal
// to decimal
static int octalToDecimal(int n)
{
  int num = n;
  int dec_value = 0;
 
  // Initializing base value 
  // to 1, i.e 8^0
  int b_ase = 1;
 
  int temp = num;
  while (temp > 0) 
  {
    // Extracting last digit
    int last_digit = temp % 10;
    temp = temp / 10;
 
    // Multiplying last digit 
    // with appropriate base 
    // value and adding it to 
    // dec_value
    dec_value += last_digit * b_ase;
 
    b_ase = b_ase * 8;
  }
   
  return dec_value;
}
 
// Function to convert decimal
// to hexadecimal
static string decToHexa(int n)
{ 
  // char array to store 
  // hexadecimal number
  char []hexaDeciNum = new char[100];
 
  // counter for hexadecimal
  // number array
  int i = 0;
  string ans = "";
 
  while(n != 0)
  { 
    // temporary variable to 
    // store remainder
    int temp = 0;
 
    // storing remainder
    // in temp variable.
    temp = n % 16;
 
    // check if temp < 10
    if(temp < 10)
    {
      hexaDeciNum[i] = (char)(temp + 48);
      i++;
    }
    else
    {
      hexaDeciNum[i] = (char)(temp + 87);
      i++;
    }
 
    n = n / 16;
  }
 
  for(int j = i - 1; j >= 0; j--)
  {
    ans += hexaDeciNum[j];
  }
   
  return ans;
}
   
// Driver code
public static void Main(string []args)
{
  string octnum, hexnum;
  int decnum;
 
  // Taking 5123 as an
  // example of Octal Number.
  octnum = "5123";
 
  // Convert Octal to Decimal
  decnum = octalToDecimal(Int32.Parse(octnum));
 
  // Convert Decimal to Hexadecimal
  hexnum = decToHexa(decnum);
 
  Console.Write("Equivalent Hexadecimal Value = " +
                 hexnum);
}
}
 
// This code is contributed by rutvik_56


输出:
Equivalent Hexadecimal Value = a53