import java.util.Arrays; /** * Byte[]與hex的相互轉換 * @explain * @author Marydon * @creationTime 2018年6月11日下午2:29:11 * @version 1.0 * @since * @email marydon20170307@163.com */ public class ByteUtils { // 16進制字符 private static final char[] HEX_CHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; }
方法一html
/** * 方法一:將byte類型數組轉化成16進制字符串 * @explain 字符串拼接 * @param bytes * @return */ public static String toHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); int num; for (byte b : bytes) { num = b < 0 ? 256 + b : b; sb.append(HEX_CHAR[num / 16]).append(HEX_CHAR[num % 16]); } return sb.toString(); }
方法二java
/** * 方法二: byte[] to hex string * @explain 使用數組 * @param bytes * @return */ public static String toHexString2(byte[] bytes) { // 一個byte爲8位,可用兩個十六進制位表示 char[] buf = new char[bytes.length * 2]; int a = 0; int index = 0; // 使用除與取餘進行轉換 for (byte b : bytes) { if (b < 0) a = 256 + b; else a = b; // 偶數位用商表示 buf[index++] = HEX_CHAR[a / 16]; // 奇數位用餘數表示 buf[index++] = HEX_CHAR[a % 16]; } // char[]-->String return new String(buf); }
方法三web
/** * 方法三: byte[]-->hexString * @explain 使用位運算 * @param bytes * @return */ public static String toHexString3(byte[] bytes) { char[] buf = new char[bytes.length * 2]; int index = 0; // 利用位運算進行轉換,能夠看做方法二的變型 for (byte b : bytes) { buf[index++] = HEX_CHAR[b >>> 4 & 0xf]; buf[index++] = HEX_CHAR[b & 0xf]; } return new String(buf); }
方法四json
/** * 方法四:byte[]-->hexString * @param bytes * @return */ public static String toHexString4(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 2); // 使用String的format方法進行轉換 for (byte b : bytes) { sb.append(String.format("%02x", new Integer(b & 0xff))); } return sb.toString(); }
方法五數組
/** * 將byte數組轉換成16進制字符串 * * @param src * @return */ private static String bytesToHexString(byte[] src) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { sb.append(0); } sb.append(hv); } return sb.toString(); }
方法一app
/** * 將16進制字符串轉換爲byte[] * @explain 16進制字符串不區分大小寫,返回的數組相同 * @param hexString * 16進制字符串 * @return byte[] */ public static byte[] fromHexString(String hexString) { if (null == hexString || "".equals(hexString.trim())) { return new byte[0]; } byte[] bytes = new byte[hexString.length() / 2]; // 16進制字符串 String hex; for (int i = 0; i < hexString.length() / 2; i++) { // 每次截取2位 hex = hexString.substring(i * 2, i * 2 + 2); // 16進制-->十進制 bytes[i] = (byte) Integer.parseInt(hex, 16); } return bytes; }
方法二測試
/** * 將16進制轉換爲byte[] * @param hexStr * @return */ public static byte[] fromHex(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; }
方法三:ui
public static byte[] toByteArray(String data) { if (data == null) { return new byte[] {}; } if (data.length() == 0) { return new byte[] {}; } while (data.length() < 2) { data = "0" + data; } if (data.substring(0, 2).toLowerCase().equals("0x")) { data = data.substring(2); } if (data.length() % 2 == 1) { data = "0" + data; } data = data.toUpperCase(); byte[] bytes = new byte[data.length() / 2]; String hexString = "0123456789ABCDEF"; for (int i = 0; i < bytes.length; i++) { int byteConv = hexString.indexOf(data.charAt(i * 2)) * 0x10; byteConv += hexString.indexOf(data.charAt(i * 2 + 1)); bytes[i] = (byte) (byteConv & 0xFF); } return bytes; }
public static void main(String[] args) throws Exception { String json = "{\"name\":\"Marydon\",\"website\":\"http://www.cnblogs.com/Marydon20170307\"}"; byte[] bytes = json.getBytes("utf-8"); System.out.println("字節數組爲:" + Arrays.toString(bytes)); System.out.println("byte數組轉16進制之方法一:" + toHexString(bytes)); System.out.println("byte數組轉16進制之方法二:" + ByteUtils.toHexString2(bytes)); System.out.println("byte數組轉16進制之方法三:" + ByteUtils.toHexString3(bytes)); System.out.println("byte數組轉16進制之方法四:" + ByteUtils.toHexString4(bytes)); System.out.println("=================================="); String str = "7b226e616d65223a224d617279646f6e222c2277656273697465223a22687474703a2f2f7777772e636e626c6f67732e636f6d2f4d617279646f6e3230313730333037227d"; System.out.println("轉換後的字節數組:" + Arrays.toString(fromHexString(str))); System.out.println(new String(fromHexString(str), "utf-8")); }