Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created December 10, 2020 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/1582eb9275ed3120fe0d847cf15119c8 to your computer and use it in GitHub Desktop.
Save parzibyte/1582eb9275ed3120fe0d847cf15119c8 to your computer and use it in GitHub Desktop.
"""
https://parzibyte.me/blog
"""
# Función que regresa el verdadero valor hexadecimal.
# Por ejemplo, si recibe un 15 devuelve f, y si recibe un número menor a 10, devuelve el número sin modificarlo
def obtener_caracter_hexadecimal(valor):
# Lo necesitamos como cadena
valor = str(valor)
equivalencias = {
"10": "a",
"11": "b",
"12": "c",
"13": "d",
"14": "e",
"15": "f",
}
if valor in equivalencias:
return equivalencias[valor]
else:
return valor
def decimal_a_hexadecimal(decimal):
hexadecimal = ""
while decimal > 0:
residuo = decimal % 16
verdadero_caracter = obtener_caracter_hexadecimal(residuo)
hexadecimal = verdadero_caracter + hexadecimal
decimal = int(decimal / 16)
return hexadecimal
decimal = int(
input("Escribe un número decimal, yo lo convertiré a hexadecimal: "))
hexadecimal = decimal_a_hexadecimal(decimal)
print(f"El decimal {decimal} es {hexadecimal} en hexadecimal")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment