Skip to content

Instantly share code, notes, and snippets.

@ErnestoRB
Created April 14, 2022 00:15
Show Gist options
  • Save ErnestoRB/e2c3a68baa54d744b485c92631d540b4 to your computer and use it in GitHub Desktop.
Save ErnestoRB/e2c3a68baa54d744b485c92631d540b4 to your computer and use it in GitHub Desktop.
Typescript decimal to hexadecimal conversion
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;
}
@ErnestoRB
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment