package org.springblade.common.utils;

/**
 * @author lidexi
 * @date 2021/5/28 9:19
 *
 */

public class ByteConvert {

	/**
	 * 字节位置倒置（两个字节为一个单位）
	 * @param chars
	 */
	public static void swap(char[] chars) {
		if (chars.length == 2) {
			return;
		}
		int start = 0;
		int end = chars.length - 1;
		int middle = (start + end) / 2;

		for (int i = 0; i <= middle; i++) {
			char temp = chars[i];
			chars[i] = chars[i + middle + 1];
			chars[i + middle + 1] = temp;
		}

		char[] left = new char[middle + 1], right = new char[middle + 1];
		for (int i = 0; i <= middle; i++) {
			left[i] = chars[i];
			right[i] = chars[i + middle + 1];
		}
		swap(left);
		swap(right);
		for (int i = 0; i <= middle; i++) {
			chars[i] = left[i];
			chars[i + middle + 1] = right[i];
		}

	}

	/**
	 * 将16进制转换为二进制
	 *
	 * @param hexStr
	 * @return
	 */
	public static String parseHexStr2Byte(String hexStr) {
		Integer num = Integer.parseInt(hexStr,16);
		String binary = Integer.toBinaryString(num);

		if (binary.length() <8) {
			while ((binary.length() < 8)) {
				StringBuffer sb = new StringBuffer();
				sb.append("0").append(binary);//左补0
//    sb.append(str).append("0");//右补0
				binary = sb.toString();

			}
		}
		return binary;
	}
}
