检查这个
public class IntToHexa {
public static void main(java.lang.String args[]){
/*
* Here we need an integer to convert.
* [1]You can pass as command line argument
* [2]You can get as input from console
* [3]Take a constant. Here I'm taking a constant
*/
int intToConvert = 450;
java.lang.StringBuilder convertedHexa = new java.lang.StringBuilder("");
while (intToConvert > 15){
/*
* If the reminder is less than 10, add the remainder. else get the equivalent hexa code
* Here I'm getting the character code and adding the charater to the hexa string.
* For that I'm getting the difference between the reminder and 10.
* For example, if the reminder is 13, the reminder will be 3.
* Then add that difference to 65. In this example, it will become 68.
* Finally, get the quivalent char code of the result number. Here it will be D.
* Same for number, I'm adding it to 48
*/
convertedHexa.append(intToConvert % 16 < 10 ? ((char)(48 + (intToConvert % 16))) : ((char)(65 + (intToConvert % 16 - 10))));
intToConvert /= 16;
}
convertedHexa.append(intToConvert % 16 < 10 ? ((char)(48 + (intToConvert % 16))) : ((char)(65 + (intToConvert % 16 - 10))));
java.lang.System.out.println(convertedHexa.reverse());
}
}