| | |
| | | package com.ard.utils; |
| | | |
| | | import javax.xml.bind.DatatypeConverter; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.nio.ByteBuffer; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.zip.CRC32; |
| | | |
| | | /** |
| | | * @Description: 字节工具类 |
| | |
| | | public class ByteUtils { |
| | | /** |
| | | * Byte字节转Hex |
| | | * |
| | | * @param b 字节 |
| | | * @return Hex |
| | | */ |
| | | public static String byteToHex(byte b) |
| | | { |
| | | public static String byteToHex(byte b) { |
| | | String hexString = Integer.toHexString(b & 0xFF); |
| | | //由于十六进制是由0~9、A~F来表示1~16,所以如果Byte转换成Hex后如果是<16,就会是一个字符(比如A=10),通常是使用两个字符来表示16进制位的, |
| | | //假如一个字符的话,遇到字符串11,这到底是1个字节,还是1和1两个字节,容易混淆,如果是补0,那么1和1补充后就是0101,11就表示纯粹的11 |
| | | if (hexString.length() < 2) |
| | | { |
| | | if (hexString.length() < 2) { |
| | | hexString = new StringBuilder(String.valueOf(0)).append(hexString).toString(); |
| | | } |
| | | return hexString.toUpperCase(); |
| | | } |
| | | |
| | | /** |
| | | * byte数组转float |
| | | */ |
| | |
| | | ByteBuffer buffer = ByteBuffer.wrap(bytes); |
| | | return buffer.getFloat(); |
| | | } |
| | | |
| | | /** |
| | | * byte数组转整型 |
| | | */ |
| | |
| | | return littleEndianBytes; |
| | | } |
| | | |
| | | /** |
| | | * 小端转大端 |
| | | */ |
| | | public static byte[] toBigEndian(byte[] littleEndianBytes) { |
| | | byte[] bigEndianBytes = new byte[littleEndianBytes.length]; |
| | | for (int i = 0; i < littleEndianBytes.length; i++) { |
| | | bigEndianBytes[i] = littleEndianBytes[littleEndianBytes.length - 1 - i]; |
| | | } |
| | | return bigEndianBytes; |
| | | } |
| | | |
| | | /** |
| | | * int转byte数组 |
| | | */ |
| | | public static byte[] decimalToBytes(int decimalValue) { |
| | | ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); |
| | | buffer.putInt(decimalValue); |
| | | return buffer.array(); |
| | | } |
| | | |
| | | /** |
| | | * byte数组拼接 |
| | | */ |
| | | public static byte[] appendArrays(byte[]... arrays) { |
| | | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| | | try { |
| | | for (byte[] array : arrays) { |
| | | outputStream.write(array); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return outputStream.toByteArray(); |
| | | } |
| | | |
| | | /** |
| | | * byte数组CRC32校验 |
| | | */ |
| | | public static byte[] parseCrc32(byte[] bytes) { |
| | | CRC32 crc32 = new CRC32(); |
| | | crc32.update(bytes); |
| | | bytes = ByteUtils.decimalToBytes((int) crc32.getValue()); |
| | | bytes = ByteUtils.toBigEndian(bytes); |
| | | // System.out.print("校验:"+DatatypeConverter.printHexBinary(bytes));//打印crc32的校验值 |
| | | return bytes; |
| | | } |
| | | |
| | | /** |
| | | * 去除包头包尾 |
| | | * 刘苏义 |
| | | * 2023/7/4 11:24 |
| | | */ |
| | | public static byte[] removeHeaderAndFooter(byte[] data, int headerLength, int footerLength) { |
| | | int payloadLength = data.length - headerLength - footerLength; |
| | | byte[] payload = new byte[payloadLength]; |
| | | System.arraycopy(data, headerLength, payload, 0, payloadLength); |
| | | return payload; |
| | | } |
| | | |
| | | /** |
| | | * 去除包头包尾crc校验 |
| | | * 刘苏义 |
| | | * 2023/7/4 11:24 |
| | | */ |
| | | public static byte[] removeHeaderFooterAndCRC(byte[] data, int headerLength, int footerLength, int crcLength) { |
| | | int payloadLength = data.length - headerLength - footerLength - crcLength; |
| | | byte[] payload = new byte[payloadLength]; |
| | | System.arraycopy(data, headerLength, payload, 0, payloadLength); |
| | | return payload; |
| | | } |
| | | |
| | | /** |
| | | * 获取后面的字节数组 |
| | | * 刘苏义 |
| | | * 2023/7/4 11:24 |
| | | */ |
| | | public static byte[] getLastBytes(byte[] data, int count) { |
| | | int startIndex = data.length - count; |
| | | byte[] lastBytes = new byte[count]; |
| | | System.arraycopy(data, startIndex, lastBytes, 0, count); |
| | | return lastBytes; |
| | | } |
| | | |
| | | /** |
| | | * 找到00的索引位置 |
| | | */ |
| | | public static int findIndexOfDoubleZero(byte[] bytes) { |
| | | for (int i = 0; i < bytes.length - 1; i++) { |
| | | if (bytes[i] == 0x00) { |
| | | return i; |
| | | } |
| | | } |
| | | return -1; |
| | | } |
| | | } |