Java将整数转换十六进制代码_Java中把整数值转换成十六进制的最佳代码

检查这个

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());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值