#字节转为16进制输出
字节码文件通过hexadecimal格式打开时,显示的为16进制的字符表示
每一个16进制串代表连个字节例如-84 -19
两个字节表示为aced
private static int readTimes = 1;
public static void readByte2Hex(byte[] bs) {
byte[] buffer = new byte[2];
int tmp1, tmp2, tmp3;
for (int i = 0; i < bs.length; i++) {
if (i % 2 == 0) {
buffer[0] = bs[i];
}
if (i % 2 == 1) {
buffer[1] = bs[i];
tmp1 = (int) buffer[0];
tmp1 = tmp1 << 8;
tmp1 = tmp1 & 0x0000FF00;
tmp2 = (int) buffer[1];
tmp2 = tmp2 & 0x000000FF; // 255
tmp3 = tmp1 | tmp2;
sysOutBytes(tmp3);
readTimes++;
}
}
}
private static void sysOutBytes(int tmp3) {
System.out.println(tmp3);
String hex = Integer.toHexString(tmp3);
while (hex.length() < 4) {
hex = "0" + hex;
}
System.out.print(hex + " ");
if (readTimes % 8 == 0) {
System.out.println();
}
}
来源:oschina
链接:https://my.oschina.net/u/1453289/blog/698877