public static void printHexByByteArray(String fileName) throws IOException { FileInputStream in=new FileInputStream(fileName); byte[] buf=new byte[1*1024]; int b=in.read(buf, 0, buf.length); int j=1; for (int i = 0; i < b; i++) { if (buf[i]<=0xf) { System.out.print("0"); } System.out.print(Integer.toHexString(buf[i] & 0xff)+" "); if(j++%10==0){ System.out.println(); } } in.close(); }
把if的判断条件再限制严格一点,改为 if(buf[i] <= 0xf && buf[i] >= 0x0) 出现3位的都是最高位为1的,被识别为有符号的负数,肯定小于0xf了