System.out.printf
@Test public void printTest() throws Exception { String str = "安倍晋三已无生命体征!!"; print(str.getBytes()); } public static void print(byte[] bytes) { for (int i = 0; i < bytes.length; i++) { System.out.printf("%02X ", bytes[i]); if ((i + 1) % 8 == 0 || i + 1 == bytes.length) System.out.print("\n"); } System.out.println("-----------------"); for (int i = 0; i < bytes.length; i++) { System.out.printf("0x%02X ", bytes[i]); if ((i + 1) % 8 == 0 || i + 1 == bytes.length) System.out.print("\n"); } System.out.println("-----------------"); for (int i = 0; i < bytes.length; i++) { System.out.print(String.format("0x%02X ", bytes[i])); if ((i + 1) % 8 == 0 || i + 1 == bytes.length) System.out.print("\n"); } System.out.println("-----------------"); }
输出
E5 AE 89 E5 80 8D E6 99 8B E4 B8 89 E5 B7 B2 E6 97 A0 E7 94 9F E5 91 BD E4 BD 93 E5 BE 81 21 21 ----------------- 0xE5 0xAE 0x89 0xE5 0x80 0x8D 0xE6 0x99 0x8B 0xE4 0xB8 0x89 0xE5 0xB7 0xB2 0xE6 0x97 0xA0 0xE7 0x94 0x9F 0xE5 0x91 0xBD 0xE4 0xBD 0x93 0xE5 0xBE 0x81 0x21 0x21 ----------------- 0xE5 0xAE 0x89 0xE5 0x80 0x8D 0xE6 0x99 0x8B 0xE4 0xB8 0x89 0xE5 0xB7 0xB2 0xE6 0x97 0xA0 0xE7 0x94 0x9F 0xE5 0x91 0xBD 0xE4 0xBD 0x93 0xE5 0xBE 0x81 0x21 0x21 -----------------