java byte数组与16进制间的相互转换

java byte数组与16进制间的相互转换

CreationTime--2018年6月11日15点34分

Author:Marydon

  1 import java.util.Arrays;
  2 
  3 /**
  4  * Byte[]与hex的相互转换
  5  * @explain
  6  * @author Marydon
  7  * @creationTime 2018年6月11日下午2:29:11
  8  * @version 1.0
  9  * @since
 10  * @email [email protected]
 11  */
 12 public class ByteUtils {
 13     // 16进制字符
 14     private static final char[] HEX_CHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
 15             'f' };
 16 
 17     /**
 18      * 方法一: byte[] to hex string
 19      * @param bytes
 20      * @return
 21      */
 22     public static String bytesToHexString(byte[] bytes) {
 23         // 一个byte为8位,可用两个十六进制位表示
 24         char[] buf = new char[bytes.length * 2];
 25         int a = 0;
 26         int index = 0;
 27         for (byte b : bytes) { // 使用除与取余进行转换
 28             if (b < 0)
 29                 a = 256 + b;
 30             else
 31                 a = b;
 32 
 33             // 偶数位用商表示
 34             buf[index++] = HEX_CHAR[a / 16];
 35             // 奇数位用余数表示
 36             buf[index++] = HEX_CHAR[a % 16];
 37         }
 38 
 39         return new String(buf);
 40     }
 41 
 42     /**
 43      * 方法二: byte[] to hex string
 44      * @param bytes
 45      * @return
 46      */
 47     public static String bytesToHexString2(byte[] bytes) {
 48         char[] buf = new char[bytes.length * 2];
 49         int index = 0;
 50         for (byte b : bytes) { // 利用位运算进行转换,可以看作方法一的变种
 51             buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
 52             buf[index++] = HEX_CHAR[b & 0xf];
 53         }
 54 
 55         return new String(buf);
 56     }
 57 
 58     /**
 59      * 方法三: byte[] to hex string
 60      * @param bytes
 61      * @return
 62      */
 63     public static String bytesToHexString3(byte[] bytes) {
 64         StringBuffer sb = new StringBuffer(bytes.length * 2);
 65         for (byte b : bytes) { // 使用String的format方法进行转换
 66             sb.append(String.format("%02x", new Integer(b & 0xff)));
 67         }
 68 
 69         return sb.toString();
 70     }
 71 
 72     /**
 73      * 将16进制字符串转换为byte[]
 74      * @param str
 75      * @return
 76      */
 77     public static byte[] hexStringToBytes(String str) {
 78         if (null == str || "".equals(str.trim())) {
 79             return new byte[0];
 80         }
 81 
 82         byte[] bytes = new byte[str.length() / 2];
 83         // 16进制字符串
 84         String hex;
 85         for (int i = 0; i < str.length() / 2; i++) {
 86             // 每次截取2位
 87             hex = str.substring(i * 2, i * 2 + 2);
 88             // 16进制-->十进制
 89             bytes[i] = (byte) Integer.parseInt(hex, 16);
 90         }
 91 
 92         return bytes;
 93     }
 94 
 95     public static void main(String[] args) throws Exception {
 96         String json = "{\"name\":\"Marydon\",\"website\":\"http://www.cnblogs.com/Marydon20170307\"}";
 97         byte[] bytes = json.getBytes("utf-8");
 98         System.out.println("字节数组为:" + Arrays.toString(bytes));
 99         System.out.println("byte数组转16进制之方法一:" + bytesToHexString(bytes));
100         System.out.println("byte数组转16进制之方法二:" + bytesToHexString2(bytes));
101         System.out.println("byte数组转16进制之方法三:" + bytesToHexString3(bytes));
102         System.out.println("==================================");
103         String str = "7b226e616d65223a224d617279646f6e222c2277656273697465223a22687474703a2f2f7777772e636e626c6f67732e636f6d2f4d617279646f6e3230313730333037227d";
104         System.out.println("转换后的字节数组:" + Arrays.toString(hexStringToBytes(str)));
105         System.out.println(new String(hexStringToBytes(str), "utf-8"));
106     }
107 
108 }

 相关推荐:

猜你喜欢

转载自www.cnblogs.com/Marydon20170307/p/9167339.html