|
@@ -0,0 +1,422 @@
|
|
|
+package com.gyee.edge.gdnxak.utils;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+* @ClassName: ByteUtil
|
|
|
+* @Description: byte 工具类
|
|
|
+* @author sun
|
|
|
+ */
|
|
|
+public class ByteUtil {
|
|
|
+
|
|
|
+ public static byte[] float2Bytes(float value) {
|
|
|
+
|
|
|
+ int tempVal = Float.floatToIntBits(value);
|
|
|
+ byte[] result = new byte[4];
|
|
|
+
|
|
|
+ result[0] = (byte) tempVal;
|
|
|
+ result[1] = (byte) (tempVal >> 8);
|
|
|
+ result[2] = (byte) (tempVal >> 16);
|
|
|
+ result[3] = (byte) (tempVal >> 24);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static float bytes2Float(byte[] bytes) {
|
|
|
+
|
|
|
+ return Float.intBitsToFloat((bytes[0] & 0xff) |
|
|
|
+ ((bytes[1] & 0xff) << 8) |
|
|
|
+ ((bytes[2] & 0xff) << 16) |
|
|
|
+ ((bytes[3] & 0xff) << 24));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: intToByteArray
|
|
|
+ * @Description: int 转换成 byte数组
|
|
|
+ * @param @param i
|
|
|
+ * @param @return
|
|
|
+ * @return byte[]
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static byte[] intToByteArray(int i) {
|
|
|
+ byte[] result = new byte[4];
|
|
|
+ result[0] = (byte) ((i >> 24) & 0xFF);
|
|
|
+ result[1] = (byte) ((i >> 16) & 0xFF);
|
|
|
+ result[2] = (byte) ((i >> 8) & 0xFF);
|
|
|
+ result[3] = (byte) (i & 0xFF);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Title: shortToByteArray
|
|
|
+ * @Description: short 转换成 byte[]
|
|
|
+ * @param @param val
|
|
|
+ * @param @return
|
|
|
+ * @return byte[]
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static byte[] shortToByteArray(short val) {
|
|
|
+ byte[] b = new byte[2];
|
|
|
+ b[0] = (byte) ((val >> 8) & 0xff);
|
|
|
+ b[1] = (byte) (val & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: byteArrayToInt
|
|
|
+ * @Description: byte[] 转换成 int
|
|
|
+ * @param @param bytes
|
|
|
+ * @param @return
|
|
|
+ * @return int
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static int byteArrayToInt(byte[] bytes) {
|
|
|
+ int value = 0;
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ int shift = (3 - i) * 8;
|
|
|
+ value += (bytes[i] & 0xFF) << shift;
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: byteArrayToShort
|
|
|
+ * @Description: byte[] 转换成short
|
|
|
+ * @param @param bytes
|
|
|
+ * @param @return
|
|
|
+ * @return short
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static short byteArrayToShort(byte[] bytes) {
|
|
|
+ short value = 0;
|
|
|
+ for (int i = 0; i < 2; i++) {
|
|
|
+ int shift = (1 - i) * 8;
|
|
|
+ value += (bytes[i] & 0xFF) << shift;
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// /**
|
|
|
+// *
|
|
|
+// * @Title: listToBytes
|
|
|
+// * @Description: TODO
|
|
|
+// * @param @param byteList
|
|
|
+// * @param @return
|
|
|
+// * @return byte[]
|
|
|
+// * @throws
|
|
|
+// */
|
|
|
+// public static byte[] listToBytes(List<Byte> byteList) {
|
|
|
+// byte[] bytes = new byte[byteList.size()];
|
|
|
+// int index = 0;
|
|
|
+// for (Byte item : byteList) {
|
|
|
+// bytes[index++] = item;
|
|
|
+// }
|
|
|
+// return bytes;
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: date2HByte
|
|
|
+ * @Description: 日期转换成 CP56Time2a
|
|
|
+ * @param @param date
|
|
|
+ * @param @return
|
|
|
+ * @return byte[]
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static byte[] date2Hbyte(Date date) {
|
|
|
+ ByteArrayOutputStream bOutput = new ByteArrayOutputStream();
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ // 毫秒需要转换成两个字节其中 低位在前高位在后
|
|
|
+ // 先转换成short
|
|
|
+ int millisecond = calendar.get(Calendar.SECOND) * 1000 + calendar.get(Calendar.MILLISECOND);
|
|
|
+
|
|
|
+ // 默认的高位在前
|
|
|
+ byte[] millisecondByte = intToByteArray(millisecond);
|
|
|
+ bOutput.write((byte) millisecondByte[3]);
|
|
|
+ bOutput.write((byte) millisecondByte[2]);
|
|
|
+
|
|
|
+ // 分钟 只占6个比特位 需要把前两位置为零
|
|
|
+ bOutput.write((byte) calendar.get(Calendar.MINUTE));
|
|
|
+ // 小时需要把前三位置零
|
|
|
+ bOutput.write((byte) calendar.get(Calendar.HOUR_OF_DAY));
|
|
|
+ // 星期日的时候 week 是0
|
|
|
+ int week = calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
+ if (week == Calendar.SUNDAY) {
|
|
|
+ week = 7;
|
|
|
+ } else {
|
|
|
+ week--;
|
|
|
+ }
|
|
|
+ // 前三个字节是 星期 因此需要将星期向左移5位 后五个字节是日期 需要将两个数字相加 相加之前需要先将前三位置零
|
|
|
+ bOutput.write((byte) (week << 5) + (calendar.get(Calendar.DAY_OF_MONTH)));
|
|
|
+ // 前四字节置零
|
|
|
+ bOutput.write((byte) ((byte) calendar.get(Calendar.MONTH) + 1));
|
|
|
+ bOutput.write((byte) (calendar.get(Calendar.YEAR) - 2000));
|
|
|
+ return bOutput.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: date2HByte
|
|
|
+ * @Description:CP56Time2a转换成 时间
|
|
|
+ * @param @param date
|
|
|
+ * @param @return
|
|
|
+ * @return byte[]
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static Date byte2Hdate(byte[] dataByte) {
|
|
|
+ int year = (dataByte[6] & 0x7F) + 2000;
|
|
|
+ int month = dataByte[5] & 0x0F;
|
|
|
+ int day = dataByte[4] & 0x1F;
|
|
|
+ int hour = dataByte[3] & 0x1F;
|
|
|
+ int minute = dataByte[2] & 0x3F;
|
|
|
+ int second = dataByte[1] > 0 ? dataByte[1] : (int) (dataByte[1] & 0xff);
|
|
|
+ int millisecond = dataByte[0] > 0 ? dataByte[0] : (int) (dataByte[0] & 0xff);
|
|
|
+ millisecond = (second << 8) + millisecond;
|
|
|
+ second = millisecond / 1000;
|
|
|
+ millisecond = millisecond % 1000;
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.YEAR, year);
|
|
|
+ calendar.set(Calendar.MONTH, month);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH, day);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, hour);
|
|
|
+ calendar.set(Calendar.MINUTE, minute);
|
|
|
+ calendar.set(Calendar.SECOND, second);
|
|
|
+ calendar.set(Calendar.MILLISECOND, millisecond);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteArrayToHexString(byte[] array) {
|
|
|
+ return byteArray2HexString(array, Integer.MAX_VALUE, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteArray2HexString(byte[] arrBytes, int count, boolean blank) {
|
|
|
+ String ret = "";
|
|
|
+ if (arrBytes == null || arrBytes.length < 1) {
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ if (count > arrBytes.length) {
|
|
|
+ count = arrBytes.length;
|
|
|
+ }
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ ret = Integer.toHexString(arrBytes[i] & 0xFF).toUpperCase();
|
|
|
+ if (ret.length() == 1) {
|
|
|
+ builder.append("0").append(ret);
|
|
|
+ } else {
|
|
|
+ builder.append(ret);
|
|
|
+ }
|
|
|
+ if (blank) {
|
|
|
+ builder.append(" ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return builder.toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回指定位置的数组
|
|
|
+ * @param bytes
|
|
|
+ * @param start 开始位置
|
|
|
+ * @param length 截取长度
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] getByte(byte[] bytes, int start, int length) {
|
|
|
+ byte[] ruleByte = new byte[length];
|
|
|
+ int index = 0;
|
|
|
+ while (index < length) {
|
|
|
+ ruleByte[index++] = bytes[start++];
|
|
|
+ }
|
|
|
+ return ruleByte;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将int转为高字节在前,低字节在后的byte数组(大端)
|
|
|
+ * @param n int
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ public static byte[] intToByteBig(int n) {
|
|
|
+ byte[] b = new byte[4];
|
|
|
+ b[3] = (byte) (n & 0xff);
|
|
|
+ b[2] = (byte) (n >> 8 & 0xff);
|
|
|
+ b[1] = (byte) (n >> 16 & 0xff);
|
|
|
+ b[0] = (byte) (n >> 24 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 将int转为低字节在前,高字节在后的byte数组(小端)
|
|
|
+ * @param n int
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ public static byte[] intToByteLittle(int n) {
|
|
|
+ byte[] b = new byte[4];
|
|
|
+ b[0] = (byte) (n & 0xff);
|
|
|
+ b[1] = (byte) (n >> 8 & 0xff);
|
|
|
+ b[2] = (byte) (n >> 16 & 0xff);
|
|
|
+ b[3] = (byte) (n >> 24 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * byte数组到int的转换(小端)
|
|
|
+ * @param bytes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int bytes2IntLittle(byte[] bytes )
|
|
|
+ {
|
|
|
+ int int1=bytes[0]&0xff;
|
|
|
+ int int2=(bytes[1]&0xff)<<8;
|
|
|
+ int int3=(bytes[2]&0xff)<<16;
|
|
|
+ int int4=(bytes[3]&0xff)<<24;
|
|
|
+
|
|
|
+ return int1|int2|int3|int4;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * byte数组到int的转换(大端)
|
|
|
+ * @param bytes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int bytes2IntBig(byte[] bytes )
|
|
|
+ {
|
|
|
+ int int1=bytes[3]&0xff;
|
|
|
+ int int2=(bytes[2]&0xff)<<8;
|
|
|
+ int int3=(bytes[1]&0xff)<<16;
|
|
|
+ int int4=(bytes[0]&0xff)<<24;
|
|
|
+
|
|
|
+ return int1|int2|int3|int4;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 将short转为高字节在前,低字节在后的byte数组(大端)
|
|
|
+ * @param n short
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ public static byte[] shortToByteBig(short n) {
|
|
|
+ byte[] b = new byte[2];
|
|
|
+ b[1] = (byte) (n & 0xff);
|
|
|
+ b[0] = (byte) (n >> 8 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将short转为低字节在前,高字节在后的byte数组(小端)
|
|
|
+ * @param n short
|
|
|
+ * @return byte[]
|
|
|
+ */
|
|
|
+ public static byte[] shortToByteLittle(short n) {
|
|
|
+ byte[] b = new byte[2];
|
|
|
+ b[0] = (byte) (n & 0xff);
|
|
|
+ b[1] = (byte) (n >> 8 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 读取小端byte数组为short
|
|
|
+ * @param b
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static short byteToShortLittle(byte[] b) {
|
|
|
+ return (short) (((b[1] << 8) | b[0] & 0xff));
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 读取大端byte数组为short
|
|
|
+ * @param b
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static short byteToShortBig(byte[] b) {
|
|
|
+ return (short) (((b[0] << 8) | b[1] & 0xff));
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * long类型转byte[] (大端)
|
|
|
+ * @param n
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] longToBytesBig(long n) {
|
|
|
+ byte[] b = new byte[8];
|
|
|
+ b[7] = (byte) (n & 0xff);
|
|
|
+ b[6] = (byte) (n >> 8 & 0xff);
|
|
|
+ b[5] = (byte) (n >> 16 & 0xff);
|
|
|
+ b[4] = (byte) (n >> 24 & 0xff);
|
|
|
+ b[3] = (byte) (n >> 32 & 0xff);
|
|
|
+ b[2] = (byte) (n >> 40 & 0xff);
|
|
|
+ b[1] = (byte) (n >> 48 & 0xff);
|
|
|
+ b[0] = (byte) (n >> 56 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * long类型转byte[] (小端)
|
|
|
+ * @param n
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] longToBytesLittle(long n) {
|
|
|
+ byte[] b = new byte[8];
|
|
|
+ b[0] = (byte) (n & 0xff);
|
|
|
+ b[1] = (byte) (n >> 8 & 0xff);
|
|
|
+ b[2] = (byte) (n >> 16 & 0xff);
|
|
|
+ b[3] = (byte) (n >> 24 & 0xff);
|
|
|
+ b[4] = (byte) (n >> 32 & 0xff);
|
|
|
+ b[5] = (byte) (n >> 40 & 0xff);
|
|
|
+ b[6] = (byte) (n >> 48 & 0xff);
|
|
|
+ b[7] = (byte) (n >> 56 & 0xff);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * byte[]转long类型(小端)
|
|
|
+ * @param array
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long bytesToLongLittle( byte[] array )
|
|
|
+ {
|
|
|
+ return ((((long) array[ 0] & 0xff) << 0)
|
|
|
+ | (((long) array[ 1] & 0xff) << 8)
|
|
|
+ | (((long) array[ 2] & 0xff) << 16)
|
|
|
+ | (((long) array[ 3] & 0xff) << 24)
|
|
|
+ | (((long) array[ 4] & 0xff) << 32)
|
|
|
+ | (((long) array[ 5] & 0xff) << 40)
|
|
|
+ | (((long) array[ 6] & 0xff) << 48)
|
|
|
+ | (((long) array[ 7] & 0xff) << 56));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * byte[]转long类型(大端)
|
|
|
+ * @param array
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long bytesToLongBig( byte[] array )
|
|
|
+ {
|
|
|
+ return ((((long) array[ 0] & 0xff) << 56)
|
|
|
+ | (((long) array[ 1] & 0xff) << 48)
|
|
|
+ | (((long) array[ 2] & 0xff) << 40)
|
|
|
+ | (((long) array[ 3] & 0xff) << 32)
|
|
|
+ | (((long) array[ 4] & 0xff) << 24)
|
|
|
+ | (((long) array[ 5] & 0xff) << 16)
|
|
|
+ | (((long) array[ 6] & 0xff) << 8)
|
|
|
+ | (((long) array[ 7] & 0xff) << 0));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组转化为16进制字符串形式
|
|
|
+ * @param b byte数组
|
|
|
+ * @return 16进制字符串
|
|
|
+ */
|
|
|
+ public static String bytes2HexString(byte[] b) {
|
|
|
+ String ret = "";
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ String hex = Integer.toHexString(b[i] & 0xFF);
|
|
|
+ if (hex.length() == 1) {
|
|
|
+ hex = '0' + hex;
|
|
|
+ }
|
|
|
+ ret += hex;
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|