Programa para convertir decimal a hexadecimal

Dado un número decimal como entrada, necesitamos escribir un programa para convertir el número decimal dado en un número hexadecimal equivalente. es decir, convertir el número con valor base 10 al valor base 16.

Los números hexadecimales utilizan 16 valores para representar un número. Los números del 0 al 9 se expresan con dígitos del 0 al 9 y del 10 al 15 con caracteres de la A a la F.

C++

// C++ program to convert a decimal
// number to hexadecimal number
 
#include <iostream>
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

<?php
// PHP program to convert
// a decimal number to
// hexadecimal number
 
// function to convert
// decimal to hexadecimal
function decToHexa($n)
{
    // char array to store
    // hexadecimal number
    $hexaDeciNum;
     
    // 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++;
        }
        else
        {
            $hexaDeciNum[$i] = chr($temp + 55);
            $i++;
        }
         
        $n = (int)($n / 16);
    }
     
    // printing hexadecimal number
    // array in reverse order
    for($j = $i - 1; $j >= 0; $j--)
        echo $hexaDeciNum[$j];
}
 
// Driver Code
$n = 2545;
decToHexa($n);
 
// This code is contributed
// by mits.
?>

Javascript

<script>
 
// Javascript program to convert a decimal
// number to hexadecimal number
 
// function to convert decimal to hexadecimal
function decToHexa(n)
{
    // char array to store hexadecimal number
    var hexaDeciNum = Array.from({length: 100},
                      (_, i) => 0);
 
    // counter for hexadecimal number array
    var i = 0;
    while (n != 0) {
        // temporary variable to store remainder
        var temp = 0;
 
        // storing remainder in temp variable.
        temp = n % 16;
 
        // check if temp < 10
        if (temp < 10) {
            hexaDeciNum[i] =
            String.fromCharCode(temp + 48);
            i++;
        }
        else {
            hexaDeciNum[i] =
            String.fromCharCode(temp + 55);
            i++;
        }
 
        n = parseInt(n / 16);
    }
 
    // printing hexadecimal number array in reverse
    // order
    for (j = i - 1; j >= 0; j--)
        document.write(hexaDeciNum[j]);
}
 
// driver program
var n = 2545;
decToHexa(n);
 
// This code contributed by shikhasingrajput
 
</script>

C++

// C++ program to convert decimal number to hexadecimal
#include <iostream>
using namespace std;
 
// function to convert decimal number to hexadecimal
void decToHexa(int n) { cout << hex << n << endl; }
 
// driver code
int main()
{
    int n = 2545;
    decToHexa(n);
    return 0;
}
 
// This code is contributed by phasing17.

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);
    }
}

Python3

# Python program to convert a decimal
# number to hexadecimal number
 
# function to convert decimal number
# to equivalent hexadecimal number
def decToHexa(n):
  return hex(n).replace("0x","")
 
# Driver Code
n = 2545
print(decToHexa(n))
 
# This code is contributed by shahidedu7.

C#

// C# program to convert a decimal
// number to hexadecimal number
using System;
 
class GFG {
    public static void decToHexa(int n)
    {
        Console.Write(Convert.ToString(n));
    }
    public static void Main(String[] args)
    {
 
        int n = 2545;
        decToHexa(n);
    }
}
 
// This code is contributed by shivanisinghss2110

Javascript

<script>
// JS program to convert a decimal
// number to hexadecimal number
 
// Function to convert decimal to hexadecimal base integer
function decToHexa(n)
{
    document.write(n.toString(16));
}
 
// Driver Code
var n = 2545;
decToHexa(n);
 
// This code is contributed by phasing17
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *