/* ............... START ............... */
public class JavaDecimalToHex {
public static void main(String[] args) {
System.out.println("Decimal 1234: Hex " + dec2hex(1234));
System.out.println("Decimal 255: Hex " + dec2hex(255));
System.out.println("Decimal 1024: Hex " + dec2hex(1024));
}
static String dec2hex(int decimal) {
char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
int x = decimal;
int r = 0;
String results = "";
while (x != 0) {
r = x % 16;
x = (int) Math.floor(x / 16);
results = hex[r] + results;
}
return (results);
}
}
/* ............... END ............... */