Created
April 14, 2022 00:15
-
-
Save ErnestoRB/e2c3a68baa54d744b485c92631d540b4 to your computer and use it in GitHub Desktop.
Typescript decimal to hexadecimal conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function conversion(decimal: number): string { | |
let resultado: string = ""; | |
const arreglo: number[] = []; | |
do { | |
arreglo.push(decimal % 16); | |
decimal = Math.floor(decimal / 16); | |
} while (decimal !== 0); | |
for (const digito of arreglo.reverse()) { | |
switch (digito) { | |
case 10: | |
resultado += "A"; | |
break; | |
case 11: | |
resultado += "B"; | |
break; | |
case 12: | |
resultado += "C"; | |
break; | |
case 13: | |
resultado += "D"; | |
break; | |
case 14: | |
resultado += "E"; | |
break; | |
case 15: | |
resultado += "F"; | |
break; | |
default: | |
resultado += digito.toString(); | |
break; | |
} | |
} | |
return resultado; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fuente: https://lasmatesfaciles.com/2021/04/27/convertir-decimal-a-hexadecimal/