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

📅  最后修改于: 2021-04-29 08:16:23             🧑  作者: Mango

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

十六进制数字使用16个值表示一个数字。 0-9的数字由数字0-9和10-15表示,由A – F字符表示。

例子:

Input : 116
Output: 74

Input : 10
Output: A

Input : 33
Output: 21

算法

  1. 在临时变量temp中将数字除以16时,存储余数。如果temp小于10,则在字符数组中插入(48 + temp),否则,如果temp大于或等于10,则在字符数组中插入(55 + temp)。
  2. 现在将数字除以16
  3. 重复上述两个步骤,直到数字不等于0。
  4. 现在以相反顺序打印阵列。

例子

如果给定的十进制数为2545。

步骤1 :计算2545除以16时的余数为1。因此,temp =1。由于temp小于10。因此,arr [0] = 48 +1 = 49 =’1’。
步骤2 :将2545除以16。新数字为2545/16 = 159。
步骤3 :计算159除以16时的余数是15。因此,temp =15。当temp大于10时。因此,arr [1] = 55 + 15 = 70 =’F’。
步骤4 :将159除以16。新数字为159/16 = 9。
步骤5 :计算9除以16时的余数是9。因此,temp =9。由于temp小于10。因此,arr [2] = 48 + 9 = 57 =’9’。
步骤6 :将9除以16。新数字为9/16 = 0。
步骤7 :由于number =0。停止重复步骤,并以相反顺序打印阵列。因此,等效的十六进制数为9F1。

下图显示了将十进制数2545转换为等效十六进制数的示例。

以下是上述想法的实现。

C++
// C++ program to convert a decimal
// number to hexadecimal number
 
#include 
using namespace std;
 
// function to convert decimal to hexadecimal
void 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 + 55;
            i++;
        }
 
        n = n / 16;
    }
 
    // printing hexadecimal number array in reverse order
    for (int j = i - 1; j >= 0; j--)
        cout << hexaDeciNum[j];
}
 
// Driver program to test above function
int main()
{
    int n = 2545;
 
    decToHexa(n);
 
    return 0;
}


Java
// Java program to convert a decimal
// number to hexadecimal number
import java.io.*;
 
class GFG {
    // function to convert decimal to hexadecimal
    static void decToHexa(int n)
    {
        // char array to store hexadecimal number
        char[] hexaDeciNum = new char[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] = (char)(temp + 48);
                i++;
            }
            else {
                hexaDeciNum[i] = (char)(temp + 55);
                i++;
            }
 
            n = n / 16;
        }
 
        // printing hexadecimal number array in reverse
        // order
        for (int j = i - 1; j >= 0; j--)
            System.out.print(hexaDeciNum[j]);
    }
 
    // driver program
    public static void main(String[] args)
    {
        int n = 2545;
        decToHexa(n);
    }
}
 
// Contributed by Pramod Kumar


Python3
# Python3 program to
# convert a decimal
# number to hexadecimal
# number
 
# function to convert
# decimal to hexadecimal
 
 
def decToHexa(n):
 
    # char array to store
    # hexadecimal number
    hexaDeciNum = ['0'] * 100
 
    # counter for hexadecimal
    # number array
    i = 0
    while(n != 0):
 
        # temporary variable
        # to store remainder
        temp = 0
 
        # storing remainder
        # in temp variable.
        temp = n % 16
 
        # check if temp < 10
        if(temp < 10):
            hexaDeciNum[i] = chr(temp + 48)
            i = i + 1
        else:
            hexaDeciNum[i] = chr(temp + 55)
            i = i + 1
        n = int(n / 16)
 
    # printing hexadecimal number
    # array in reverse order
    j = i - 1
    while(j >= 0):
        print((hexaDeciNum[j]), end="")
        j = j - 1
 
 
# Driver Code
n = 2545
decToHexa(n)
 
# This code is contributed
# by mits.


C#
// C# program to convert a decimal
// number to hexadecimal number
using System;
 
class GFG {
    // function to convert decimal
    // to hexadecimal
    static void decToHexa(int n)
    {
        // char array to store
        // hexadecimal number
        char[] hexaDeciNum = new char[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] = (char)(temp + 48);
                i++;
            }
            else {
                hexaDeciNum[i] = (char)(temp + 55);
                i++;
            }
 
            n = n / 16;
        }
 
        // printing hexadecimal number
        // array in reverse order
        for (int j = i - 1; j >= 0; j--)
            Console.Write(hexaDeciNum[j]);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 2545;
        decToHexa(n);
    }
}
 
// This code is contributed by Nitin Mittal.


PHP
= 0; $j--)
        echo $hexaDeciNum[$j];
}
 
// Driver Code
$n = 2545;
decToHexa($n);
 
// This code is contributed
// by mits.
?>


Java
// Java program to convert a decimal
// number to hexadecimal number
import java.io.*;
 
class GFG {
    public static void decToHexa(int n)
    {
        System.out.println(Integer.toHexString(n));
    }
    public static void main(String[] args)
    {
 
        int n = 2545;
        decToHexa(n);
    }
}


输出
9F1

使用预定义函数

Java

// Java program to convert a decimal
// number to hexadecimal number
import java.io.*;
 
class GFG {
    public static void decToHexa(int n)
    {
        System.out.println(Integer.toHexString(n));
    }
    public static void main(String[] args)
    {
 
        int n = 2545;
        decToHexa(n);
    }
}
输出
9f1