Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 26, 2020 19:05
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/eea9a2a576f7c8d8284a3bdb412dc15a to your computer and use it in GitHub Desktop.
Save parzibyte/eea9a2a576f7c8d8284a3bdb412dc15a to your computer and use it in GitHub Desktop.
/*
____ _____ _ _ _
| _ \ | __ \ (_) | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___|
__/ | __/ |
|___/ |___/
Blog: https://parzibyte.me/blog
Ayuda: https://parzibyte.me/blog/contrataciones-ayuda/
Contacto: https://parzibyte.me/blog/contacto/
Copyright (c) 2020 Luis Cabrera Benito
Licenciado bajo la licencia MIT
El texto de arriba debe ser incluido en cualquier redistribucion
*/
public class Main {
public static void main(String[] args) {
int decimales[] = { 16777215, 12, 21 };
for (int decimal : decimales) {
String hexadecimal = Integer.toHexString(decimal);
System.out.printf("Usando Integer.toHexString el decimal %d es %s\n", decimal, hexadecimal);
hexadecimal = decimalAHexadecimal(decimal);
System.out.printf("Usando decimalAHexadecimal el decimal %d es %s\n", decimal, hexadecimal);
}
}
public static String decimalAHexadecimal(int decimal) {
String hexadecimal = "";
String caracteresHexadecimales = "0123456789abcdef";
while (decimal > 0) {
int residuo = decimal % 16;
hexadecimal = caracteresHexadecimales.charAt(residuo) + hexadecimal; // Agregar a la izquierda
decimal /= 16;
}
return hexadecimal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment