使用Java完成byte陣列與十六進位制字串相互轉換

Lavi_qq_2910138025發表於2017-02-27

最簡單的轉換方法:

/**
 * @see 將byte[]陣列轉換為String字串
 * @author Herman.Xiong
 * @date 2014年5月5日 17:15:42
 * @param data byte陣列
 * @return String 轉換後的字串
 */
public static String byteToArray(byte[]data){
	String result="";
	for (int i = 0; i < data.length; i++) {
		result+=Integer.toHexString((data[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3);
	}
	return result;
}

java的byte[]陣列的原理:

Java中byte用二進位制表示佔用8位,而我們知道16進位制的每個字元需要用4位二進位制位來表示。所以我們就可以把每個byte轉換成兩個相應的16進位制字元,即把byte的高4位和低4位分別轉換成相應的16進位制字元H和L,並組合起來得到byte轉換到16進位制字串的結果new String(H) + new String(L)。同理,相反的轉換也是將兩個16進位制字元轉換成一個byte,原理同上。根據以上原理,我們就可以將byte[] 陣列轉換為16進位制字串了,當然也可以將16進位制字串轉換為byte[]陣列了。

byte陣列與十六進位制字串相互轉換工具類:

package com.herman.test;

/**
 * @see byte陣列與十六進位制字串互轉
 * @author Herman.Xiong
 * @date 2014年5月5日 17:00:01
 */
public class Hex {

	/**
	 * 用於建立十六進位制字元的輸出的小寫字元陣列
	 */
	private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	/**
	 * 用於建立十六進位制字元的輸出的大寫字元陣列
	 */
	private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	/**
	 * @see 將位元組陣列轉換為十六進位制字元陣列
	 * @author Herman.Xiong
	 * @date 2014年5月5日 17:06:52
	 * @param data byte[]
	 * @return 十六進位制char[]
	 */
	public static char[] encodeHex(byte[] data) {
		return encodeHex(data, true);
	}

	/**
	 * @see 將位元組陣列轉換為十六進位制字元陣列
	 * @author Herman.Xiong
	 * @date 2014年5月5日 17:07:14
	 * @param data byte[]
	 * @param toLowerCase true傳換成小寫格式 ,false傳換成大寫格式
	 * @return 十六進位制char[]
	 */
	public static char[] encodeHex(byte[] data, boolean toLowerCase) {
		return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
	}

	/**
	 * @see 將位元組陣列轉換為十六進位制字元陣列
	 * @author Herman.Xiong
	 * @date 2014年5月5日 17:07:31
	 * @param data byte[]
	 * @param toDigits 用於控制輸出的char[]
	 * @return 十六進位制char[]
	 */
	protected static char[] encodeHex(byte[] data, char[] toDigits) {
		int l = data.length;
		char[] out = new char[l << 1];
		for (int i = 0, j = 0; i < l; i++) {
			out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
			out[j++] = toDigits[0x0F & data[i]];
		}
		return out;
	}

	/**
	 * @see 將位元組陣列轉換為十六進位制字串
	 * @date 2014年5月5日 17:07:43
	 * @author Herman.Xiong
	 * @param data byte[]
	 * @return 十六進位制String
	 */
	public static String encodeHexStr(byte[] data) {
		return encodeHexStr(data, true);
	}

	/**
	 * @see 將位元組陣列轉換為十六進位制字串
	 * @author Herman.Xiong
	 * @date 2014年5月5日 17:08:01
	 * @param data byte[]
	 * @param toLowerCase true 傳換成小寫格式 , false 傳換成大寫格式
	 * @return 十六進位制String
	 */
	public static String encodeHexStr(byte[] data, boolean toLowerCase) {
		return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
	}

	/**
	 * @see 將位元組陣列轉換為十六進位制字串
	 * @author Herman.Xiong
	 * @date 2014年5月5日 17:08:15
	 * @param data byte[]
	 * @param toDigits 用於控制輸出的char[]
	 * @return 十六進位制String
	 */
	protected static String encodeHexStr(byte[] data, char[] toDigits) {
		return new String(encodeHex(data, toDigits));
	}

	/**
	 * @see 將十六進位制字元陣列轉換為位元組陣列
	 * @author Herman.Xiong
	 * @date 2014年5月5日 17:08:28
	 * @param data 十六進位制char[]
	 * @return byte[]
	 * @throws RuntimeException 如果源十六進位制字元陣列是一個奇怪的長度,將丟擲執行時異常
	 */
	public static byte[] decodeHex(char[] data) {
		int len = data.length;
		if ((len & 0x01) != 0) {
			throw new RuntimeException("未知的字元");
		}
		byte[] out = new byte[len >> 1];
		for (int i = 0, j = 0; j < len; i++) {
			int f = toDigit(data[j], j) << 4;
			j++;
			f = f | toDigit(data[j], j);
			j++;
			out[i] = (byte) (f & 0xFF);
		}
		return out;
	}

	/**
	 * @see 將十六進位制字元轉換成一個整數
	 * @author Herman.Xiong
	 * @date 2014年5月5日 17:08:46
	 * @param ch  十六進位制char
	 * @param index 十六進位制字元在字元陣列中的位置
	 * @return 一個整數
	 * @throws RuntimeException 當ch不是一個合法的十六進位制字元時,丟擲執行時異常
	 */
	protected static int toDigit(char ch, int index) {
		int digit = Character.digit(ch, 16);
		if (digit == -1) {
			throw new RuntimeException("非法16進位制字元 " + ch
					+ " 在索引 " + index);
		}
		return digit;
	}

	/**
	 * @see 將byte[]陣列轉換為String字串
	 * @author Herman.Xiong
	 * @date 2014年5月5日 17:15:42
	 * @param data byte陣列
	 * @return String 轉換後的字串
	 */
	public static String byteToArray(byte[]data){
		String result="";
		for (int i = 0; i < data.length; i++) {
			result+=Integer.toHexString((data[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3);
		}
		return result;
	}
	
	public static void main(String[] args) {
		String srcStr = "待轉換字串";
		String encodeStr = encodeHexStr(srcStr.getBytes());
		String decodeStr = new String(decodeHex(encodeStr.toCharArray()));
		System.out.println("轉換前:" + srcStr);
		System.out.println("轉換後:" + encodeStr);
		System.out.println("還原後:" + decodeStr);
	}

}
https://i.iter01.com/images/1778c27af6f360a03ae6d8095c56f010799d18084f8c4dab8f0a49cf18558f3c.png
來自:http://m.blog.csdn.net/article/details?id=25063627


相關文章