系统方法和自定义方法
public class Hex {
@Test
public void convert2Hex() {
for (int i = 10; i < 20; i++) {
System.out.println(i + "系统方法:" + Integer.toHexString(i));
System.out.println(i + "自定义方法:" + byteToHex(i));
}
}
/**
* 将一个整形化为十六进制,并以字符串的形式返回
*/
private final static String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
public String byteToHex(int n) {
if (n < 0) {
n = n + 256;
}
int d1 = n / 16;
int d2 = n % 16;
return hexArray[d1] + hexArray[d2];
}
}
输出结果
10系统方法:a
10自定义方法:0a
11系统方法:b
11自定义方法:0b
12系统方法:c
12自定义方法:0c
13系统方法:d
13自定义方法:0d
14系统方法:e
14自定义方法:0e
15系统方法:f
15自定义方法:0f
16系统方法:10
16自定义方法:10
17系统方法:11
17自定义方法:11
18系统方法:12
18自定义方法:12
19系统方法:13
19自定义方法:13
版权声明:本文为yu540135101原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。