不同数据类型的数据参与运算,数据类型要强制转换,转换方向是:(unsigned)char,(unsigned)short->int->unsigned->long->unsigned long->float->double。
Java代码:
public class DtoH {
public static void main(String[] args) {
toHex(60);
}
public static void toHex(int num) {
for(int x=0; x<8; x++) {
int temp = num & 15; //和 1111(二进制) 进行与运算,得到十六进制的最后一位。
if(temp>9)
System.out.println((char)(temp-10+'A')); //大于9则转换成十六进制,将10、11、12、13、14、15对应输出为 A、B、C、D、E。
else
System.out.println(temp);
num = num >>> 4; //右移四位,继续与运算。
}
}
}