62,615
社区成员
发帖
与我相关
我的任务
分享
public class Test2 {
private final static char[] HEX = "0123456789abcdef".toCharArray();
public static void main(String[] args) {
byte[] bys = { 0x7e, (byte)0x8f, 0x7c, (byte)0xa9 };
System.out.println(toHex(bys, 0, 3));
}
public static String toHex(byte[] bys, int start, int length) {
if(bys == null || bys.length == 0) {
throw new IllegalArgumentException("byte array is empty");
}
if(start < 0) {
throw new IllegalArgumentException("start is array index that greater than or equal 0");
}
if(length < 1) {
throw new IllegalArgumentException("length must be greater than 0");
}
if(start + length > bys.length) {
throw new IllegalArgumentException("array max length is " + bys.length);
}
char[] chs = new char[length * 2];
for(int i = start, k = start + length, n = 0; i < k; i++) {
chs[n++] = HEX[bys[i] >> 4 & 0xf];
chs[n++] = HEX[bys[i] & 0xf];
}
return new String(chs);
}
}