|
@@ -0,0 +1,411 @@
|
|
|
+package com.zhili.zkservicecenter.util;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author :HuangBin
|
|
|
+ * @description:TODO
|
|
|
+ * @date :2022/10/18 14:00
|
|
|
+ */
|
|
|
+public class BytesUtil {
|
|
|
+
|
|
|
+ public static final int BIT = 8;
|
|
|
+
|
|
|
+ public static byte[] fromIntWithHigherFirst(int intVal, int bytesLen) {
|
|
|
+ byte[] bytes5 = new byte[bytesLen];
|
|
|
+ while (bytesLen > 0) {
|
|
|
+ bytesLen--;
|
|
|
+ bytes5[bytesLen] = (byte) (intVal >> 8 * (bytes5.length - bytesLen - 1) & 0xFF);
|
|
|
+ }
|
|
|
+ return bytes5;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] fromIntWithLowerFirst(int intVal, int bytesLen) {
|
|
|
+ byte[] bytes5 = new byte[bytesLen];
|
|
|
+ while (bytesLen > 0) {
|
|
|
+ bytesLen--;
|
|
|
+ bytes5[bytes5.length - bytesLen - 1] = (byte) (intVal >> 8 * (bytes5.length - bytesLen - 1) & 0xFF);
|
|
|
+ }
|
|
|
+ return bytes5;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int toIntWithLowerFirst(byte[] bytes) {
|
|
|
+ int length = bytes.length;
|
|
|
+ int res = 0;
|
|
|
+ for (int i = 0; i < length; i++) {
|
|
|
+ res = res * 256;
|
|
|
+ res += Byte.toUnsignedInt(bytes[length - i - 1]);
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte sumCheck(byte[] b) {
|
|
|
+ int sum = 0;
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ sum = sum + b[i];
|
|
|
+ sum = 0xff & sum;
|
|
|
+ }
|
|
|
+ return (byte) sum;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String parseStringFromGb2312Bytes(byte[] bytes){
|
|
|
+ try{
|
|
|
+ String gb2312Str = new String(bytes, "gb2312");
|
|
|
+ return gb2312Str.substring(0,gb2312Str.indexOf((char)0));
|
|
|
+ }catch (Exception e){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String parseString(byte[] b) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ sb.append((char) b[i]);
|
|
|
+ }
|
|
|
+ String rawString = sb.toString();
|
|
|
+ int k = rawString.indexOf(0);
|
|
|
+ return rawString.substring(0, k);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] fromString(String s, int len, byte fill) {
|
|
|
+ byte[] res = new byte[len];
|
|
|
+ for (int i = 0; i < s.length(); i++) {
|
|
|
+ byte b = (byte) s.charAt(i);
|
|
|
+ res[i] = b;
|
|
|
+ }
|
|
|
+ for (int j = s.length(); j < len; j++) {
|
|
|
+ res[j] = fill;
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setSubBytes(byte[] source, int index, byte[] target) {
|
|
|
+ for (int i = 0; i < target.length; i++) {
|
|
|
+ source[index + i] = target[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] composeNowTimeBytes() {
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ String timeStr = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(now);
|
|
|
+ byte[] timeBytes = new byte[8];
|
|
|
+ for (int i = 0; i < 7; i++) {
|
|
|
+ String partStr = timeStr.substring(2 * i, 2 * (i + 1));
|
|
|
+ timeBytes[i] = BytesUtil.fromIntWithLowerFirst(Integer.valueOf(partStr, 16), 1)[0];
|
|
|
+ }
|
|
|
+ timeBytes[7] = (byte) 0xff;
|
|
|
+ return timeBytes;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] crc(byte[] bytes) {
|
|
|
+ String crcStr = getCRCStr(bytes);
|
|
|
+ String[] ss = crcStr.split(" ");
|
|
|
+ byte[] crc = new byte[ss.length];
|
|
|
+ for (int i = 0; i < ss.length; i++) {
|
|
|
+ crc[i] = (byte) Integer.valueOf(ss[i], 16).intValue();
|
|
|
+ }
|
|
|
+ return crc;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCRCStr(byte[] bytes) {
|
|
|
+ //CRC寄存器全为1
|
|
|
+ int CRC = 0x0000ffff;
|
|
|
+ //多项式校验值
|
|
|
+ int POLYNOMIAL = 0x0000a001;
|
|
|
+ int i, j;
|
|
|
+ for (i = 0; i < bytes.length; i++) {
|
|
|
+ CRC ^= ((int) bytes[i] & 0x000000ff);
|
|
|
+ for (j = 0; j < 8; j++) {
|
|
|
+ if ((CRC & 0x00000001) != 0) {
|
|
|
+ CRC >>= 1;
|
|
|
+ CRC ^= POLYNOMIAL;
|
|
|
+ } else {
|
|
|
+ CRC >>= 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //结果转换为16进制
|
|
|
+ String result = Integer.toHexString(CRC).toUpperCase();
|
|
|
+ if (result.length() != 4) {
|
|
|
+ StringBuffer sb = new StringBuffer("0000");
|
|
|
+ result = sb.replace(4 - result.length(), 4, result).toString();
|
|
|
+ }
|
|
|
+ //交换高低位
|
|
|
+ return result.substring(2, 4) + " " + result.substring(0, 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * led data封装
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ * @throws UnsupportedEncodingException
|
|
|
+ */
|
|
|
+ public static byte[] getDataByte(String str, int type) throws UnsupportedEncodingException {
|
|
|
+ System.out.println(str);
|
|
|
+ byte[] dataBytes = str.getBytes(type == 1 ? "gb2312" : "Unicode");
|
|
|
+ dataBytes = removeTheElement(dataBytes, 0);
|
|
|
+ dataBytes = removeTheElement(dataBytes, 0);
|
|
|
+ //System.out.println(bytesToHexString(dataBytes));
|
|
|
+ int l = 8 + dataBytes.length;
|
|
|
+ if (type == 1) {
|
|
|
+ l = 8 + dataBytes.length * 2;
|
|
|
+ }
|
|
|
+ byte[] data = new byte[l];
|
|
|
+ BytesUtil.setSubBytes(data, 0, BytesUtil.fromIntWithLowerFirst(1, 2));
|
|
|
+ data[2] = (byte) type;
|
|
|
+ data[3] = (byte) 2;
|
|
|
+ data[4] = (byte) 0;
|
|
|
+ data[5] = (byte) 1;
|
|
|
+ BytesUtil.setSubBytes(data, 6, BytesUtil.fromIntWithLowerFirst(dataBytes.length, 2));
|
|
|
+ if (type == 1) {
|
|
|
+ BytesUtil.setSubBytes(data, 6, BytesUtil.fromIntWithLowerFirst(dataBytes.length * 2, 2));
|
|
|
+ }
|
|
|
+ BytesUtil.setSubBytes(data, 8, dataBytes);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] removeTheElement(byte[] arr, int index) {
|
|
|
+ if (arr == null || index < 0 || index >= arr.length) {
|
|
|
+ return arr;
|
|
|
+ }
|
|
|
+ byte[] anotherArray = new byte[arr.length - 1];
|
|
|
+ for (int i = 0, k = 0; i < arr.length; i++) {
|
|
|
+ if (i == index) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ anotherArray[k++] = arr[i];
|
|
|
+ }
|
|
|
+ return anotherArray;
|
|
|
+ }
|
|
|
+
|
|
|
+ //byte数组转String
|
|
|
+ public static String bytesToHexString(byte[] bArray) {
|
|
|
+ StringBuffer sb = new StringBuffer(bArray.length);
|
|
|
+ String sTemp;
|
|
|
+ for (int i = 0; i < bArray.length; i++) {
|
|
|
+ sTemp = Integer.toHexString(0xFF & bArray[i]);
|
|
|
+ if (sTemp.length() < 2)
|
|
|
+ sb.append(0);
|
|
|
+ sb.append(sTemp.toUpperCase());
|
|
|
+ }
|
|
|
+ int length = sb.length();
|
|
|
+ if (length == 1 || length == 0) {
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ if (length % 2 == 1) {
|
|
|
+ sb.insert(length - 1, " ");
|
|
|
+ length = length - 1;
|
|
|
+ }
|
|
|
+ for (int i = length; i > 0; i = i - 2) {
|
|
|
+ sb.insert(i, " ");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * description: 字符串转 Unicode --- 原生方法
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return String
|
|
|
+ * @version v1.0
|
|
|
+ * @author w
|
|
|
+ * @date 2021年4月21日 上午10:18:25
|
|
|
+ */
|
|
|
+ public static String stringToUnicode(String str) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ char[] c = str.toCharArray();
|
|
|
+ for (int i = 0; i < c.length; i++) {
|
|
|
+ sb.append("\\u" + Integer.toHexString(c[i]));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * description: Unicode 转字符串 --- 原生方法
|
|
|
+ *
|
|
|
+ * @param unicode
|
|
|
+ * @return String
|
|
|
+ * @version v1.0
|
|
|
+ * @author w
|
|
|
+ * @date 2021年4月21日 上午10:18:57
|
|
|
+ */
|
|
|
+ public static String unicodeToString(String unicode) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ String[] hex = unicode.split("\\\\u");
|
|
|
+ for (int i = 1; i < hex.length; i++) {
|
|
|
+ int index = Integer.parseInt(hex[i], 16);
|
|
|
+ sb.append((char) index);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param data1
|
|
|
+ * @param data2
|
|
|
+ * @return data1 与 data2拼接的结果
|
|
|
+ */
|
|
|
+ public static byte[] addBytes(byte[] data1, byte[] data2) {
|
|
|
+ byte[] data3 = new byte[data1.length + data2.length];
|
|
|
+ System.arraycopy(data1, 0, data3, 0, data1.length);
|
|
|
+ System.arraycopy(data2, 0, data3, data1.length, data2.length);
|
|
|
+ return data3;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param data1
|
|
|
+ * @param data2
|
|
|
+ * @return data1 与 data2拼接的结果
|
|
|
+ */
|
|
|
+ public static byte[] addByte(byte[] data1, byte data2) {
|
|
|
+ byte[] datax = {data2};
|
|
|
+ byte[] data3 = new byte[data1.length + datax.length];
|
|
|
+ System.arraycopy(data1, 0, data3, 0, data1.length);
|
|
|
+ System.arraycopy(datax, 0, data3, data1.length, datax.length);
|
|
|
+ return data3;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回8个字节日期时间16进制
|
|
|
+ */
|
|
|
+ public static byte[] getBytesTime() {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ // 当前年
|
|
|
+ int year = cal.get(Calendar.YEAR);
|
|
|
+ // 当前月
|
|
|
+ int month = cal.get(Calendar.MONTH) + 1;
|
|
|
+ // 当前日
|
|
|
+ int day = cal.get(Calendar.DATE);
|
|
|
+ // 当前小时
|
|
|
+ int hour = cal.get(Calendar.HOUR_OF_DAY);
|
|
|
+ // 当前分钟
|
|
|
+ int minute = cal.get(Calendar.MINUTE);
|
|
|
+ // 当前秒
|
|
|
+ int second = cal.get(Calendar.SECOND);
|
|
|
+ int yearOne = Integer.valueOf(StringUtils.substring(year + "", 0, 2));
|
|
|
+ int yearTwo = Integer.valueOf(StringUtils.substring(year + "", 2, 4));
|
|
|
+ byte[] time = new byte[8];
|
|
|
+ time[0] = (byte) yearOne;
|
|
|
+ time[1] = (byte) yearTwo;
|
|
|
+ time[2] = (byte) month;
|
|
|
+ time[3] = (byte) day;
|
|
|
+ time[4] = (byte) hour;
|
|
|
+ time[5] = (byte) minute;
|
|
|
+ time[6] = (byte) second;
|
|
|
+ time[7] = (byte) 0;
|
|
|
+ return time;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String composeTimeString(byte[] timeBytes) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < timeBytes.length - 1; i++) {
|
|
|
+ byte b = timeBytes[i];
|
|
|
+ String partStr = Integer.toHexString(Byte.toUnsignedInt(b));
|
|
|
+ if (partStr.length() == 1) {
|
|
|
+ partStr = "0" + partStr;
|
|
|
+ }
|
|
|
+ sb.append(partStr);
|
|
|
+// 2015-02-13 12:00:12
|
|
|
+ if (i < 1 || i >= 6) {
|
|
|
+
|
|
|
+ } else if (i < 3) {
|
|
|
+ sb.append("-");
|
|
|
+ } else if (i < 4) {
|
|
|
+ sb.append(" ");
|
|
|
+ } else if (i < 6) {
|
|
|
+ sb.append(":");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Integer> getFault(byte[] alarmData) {
|
|
|
+ int count = alarmData.length * BIT;
|
|
|
+ List<Integer> faultList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ int bitSkip = i / BIT;
|
|
|
+ int bitIndex = i % BIT;
|
|
|
+ byte alarmDatum = alarmData[bitSkip];
|
|
|
+ byte success = (byte) 1;
|
|
|
+ success <<= bitIndex;
|
|
|
+ byte res = (byte) (alarmDatum & success);
|
|
|
+ if (Byte.valueOf(res).equals(success)) {
|
|
|
+ faultList.add(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return faultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean checkSum(byte[] data) {
|
|
|
+ byte sum = 0;
|
|
|
+ for (int i = 1; i < data.length; i++) {
|
|
|
+ if (i == 1) {
|
|
|
+ sum = data[i];
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ sum = (byte) (sum ^ data[i]);
|
|
|
+ }
|
|
|
+ byte datum = data[0];
|
|
|
+ return sum == datum;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean sensorCheckSum(byte[] data){
|
|
|
+ byte sum=0;
|
|
|
+ for (int i=0;i<data.length;i++){
|
|
|
+ if (i == 0) {
|
|
|
+ sum = data[i];
|
|
|
+ }else{
|
|
|
+ sum += data[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sum==0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String toSnString(byte[] data) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (byte datum : data) {
|
|
|
+ char sn = (char) datum;
|
|
|
+ sb.append(sn);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean bytesEqual(byte[] bytes1, byte[] bytes2) {
|
|
|
+ boolean res = true;
|
|
|
+ for (int i = 0; i < bytes1.length; i++) {
|
|
|
+ if (bytes1[i] != bytes2[i]) {
|
|
|
+ res = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int parseBits(byte data, int start, int end) {
|
|
|
+ byte mask = 0;
|
|
|
+ byte one = 1;
|
|
|
+ for (int i = 0; i < 8; i++) {
|
|
|
+ if (i >= start && i < end) {
|
|
|
+ mask += (byte) (one << i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+// System.out.println("mask:" + mask);
|
|
|
+ byte byteRes = (byte) (((mask & data) & 0xff) >> start);
|
|
|
+ return Byte.toUnsignedInt(byteRes);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSn(byte[] data, int len) {
|
|
|
+ String sn = "";
|
|
|
+ for (int i = 8; i <= len; i++) {
|
|
|
+ sn = sn + data[i];
|
|
|
+ }
|
|
|
+ return sn;
|
|
|
+ }
|
|
|
+}
|