以16进制显示2进制数据

  static String toHex(byte[] data) {
    int n = 0;
    StringBuilder buf = new StringBuilder();
    for (byte b : data) {
      if (n % 16 == 0) {
        buf.append(String.format("%05X: ", n));
      }
      buf.append(String.format("%02X ", b));
      n++;
      if (n % 16 == 0) {
        buf.append("\n");
      }
    }

    return buf.toString();
  }

测试:

 

public static void main(String[] args) {
	System.out.println(toHex("123123123123123123123123123123123123123123123".getBytes()));
}

结果:

 

00000: 31 32 33 31 32 33 31 32 33 31 32 33 31 32 33 31 
00010: 32 33 31 32 33 31 32 33 31 32 33 31 32 33 31 32 
00020: 33 31 32 33 31 32 33 31 32 33 31 32 33

你可能感兴趣的:(以16进制显示2进制数据)