|
@@ -0,0 +1,345 @@
|
|
|
+/**
|
|
|
+ *
|
|
|
+ */
|
|
|
+package com.hb.proj.server;
|
|
|
+
|
|
|
+import java.nio.ByteBuffer;
|
|
|
+import java.nio.ByteOrder;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author liyongjun
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ByteUtil
|
|
|
+{
|
|
|
+ public static short getShort(short s)
|
|
|
+ {
|
|
|
+ ByteBuffer bb = ByteBuffer.allocate(2);
|
|
|
+ bb.order(ByteOrder.BIG_ENDIAN);
|
|
|
+ bb.asShortBuffer().put(s);
|
|
|
+ bb.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
+ return bb.getShort();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getInt(int i){
|
|
|
+ ByteBuffer bb = ByteBuffer.allocate(4);
|
|
|
+ bb.order(ByteOrder.BIG_ENDIAN);
|
|
|
+ bb.asIntBuffer().put(i);
|
|
|
+ bb.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
+ return bb.getInt();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 截取byte[]
|
|
|
+ */
|
|
|
+ public static byte[] subBytes(byte[] src, int begin, int count)
|
|
|
+ {
|
|
|
+ byte[] bs = new byte[count];
|
|
|
+ for (int i = begin; i < begin + count; i++)
|
|
|
+ bs[i - begin] = src[i];
|
|
|
+ return bs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 截取byte[]--> 小端转大端(适用 多字节的转换)
|
|
|
+ */
|
|
|
+ public static byte[] subBytes2Exchange(byte[] src, int begin, int count)
|
|
|
+ {
|
|
|
+ byte[] bs = new byte[count];
|
|
|
+ for (int i = begin; i < begin + count; i++)
|
|
|
+ bs[i - begin] = src[i];
|
|
|
+ return byteExchange(bs, count);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组转换成整型
|
|
|
+ *
|
|
|
+ * @param b
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int byteArrayToInt(byte[] b)
|
|
|
+ {
|
|
|
+ int value = 0;
|
|
|
+ for (int i = 0; i < 4; i++)
|
|
|
+ {
|
|
|
+ int shift = (4 - 1 - i) * 8;
|
|
|
+ value += (b[i] & 0x000000FF) << shift;
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 截取byte[]--> 转成整形(适用于单字节转换)
|
|
|
+ *
|
|
|
+ * @param b
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int byteArrayToInt(byte[] b, int begin, int count)
|
|
|
+ {
|
|
|
+ return byteArrayToInt(subBytes(b, begin, count), count);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 截取byte[]--> 小端转大端--> 转成整型(适用多字节转换)
|
|
|
+ *
|
|
|
+ * @param b
|
|
|
+ * @param begin
|
|
|
+ * @param count
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int byteArrayExchangeToInt(byte[] b, int begin, int count)
|
|
|
+ {
|
|
|
+ return byteArrayToInt(subBytes2Exchange(b, begin, count), count);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param i
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组转换成整型
|
|
|
+ *
|
|
|
+ * @param b
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int byteArrayToInt(byte[] b, int l)
|
|
|
+ {
|
|
|
+ int value = 0;
|
|
|
+ for (int i = 0; i < l; i++)
|
|
|
+ {
|
|
|
+ int shift = (l - 1 - i) * 8;
|
|
|
+ value += (b[i] & 0x000000FF) << shift;
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Convert byte[] to hex string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int) 来转换成16进制字符串。
|
|
|
+ *
|
|
|
+ * @param src byte[] data
|
|
|
+ * @return hex string
|
|
|
+ */
|
|
|
+ public static String bytesToHexString(byte[] src)
|
|
|
+ {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder("");
|
|
|
+ if (src == null || src.length <= 0)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < src.length; i++)
|
|
|
+ {
|
|
|
+ int v = src[i] & 0xFF;
|
|
|
+ String hv = Integer.toHexString(v);
|
|
|
+ if (hv.length() < 2)
|
|
|
+ {
|
|
|
+ stringBuilder.append(0);
|
|
|
+ }
|
|
|
+ stringBuilder.append(hv);
|
|
|
+ stringBuilder.append(" ");
|
|
|
+ }
|
|
|
+ return stringBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 4字节数组大小端转换
|
|
|
+ *
|
|
|
+ * @param src
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] byteExchange(byte[] src)
|
|
|
+ {
|
|
|
+ if (src.length > 4)
|
|
|
+ {
|
|
|
+ return src;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ byte temp[] = new byte[4];
|
|
|
+ for (int i = 0; i < 4; i++)
|
|
|
+ {
|
|
|
+ temp[i] = src[4 - i - 1];
|
|
|
+ }
|
|
|
+ return temp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组大小端转换
|
|
|
+ *
|
|
|
+ * @param src
|
|
|
+ * @param l 字节长度
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] byteExchange(byte[] src, int l)
|
|
|
+ {
|
|
|
+ if (src.length > 4 || l > 4)
|
|
|
+ {
|
|
|
+ return src;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ byte temp[] = new byte[l];
|
|
|
+ for (int i = 0; i < l; i++)
|
|
|
+ {
|
|
|
+ temp[i] = src[l - i - 1];
|
|
|
+ }
|
|
|
+ return temp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节数组大小端转换
|
|
|
+ *
|
|
|
+ * @param src
|
|
|
+ * @param l 字节长度
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] subByteExchange(byte[] src, int begin, int count)
|
|
|
+ {
|
|
|
+ return byteExchange(subBytes(src, begin, count), count);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并2个byte数组
|
|
|
+ *
|
|
|
+ * @param byte_1
|
|
|
+ * @param byte_2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] byteMerger(byte[] byte_1, byte[] byte_2)
|
|
|
+ {
|
|
|
+ byte[] byte_3 = new byte[byte_1.length + byte_2.length];
|
|
|
+ System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
|
|
|
+ System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
|
|
|
+ return byte_3;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并多个个byte数组
|
|
|
+ *
|
|
|
+ * @param byte_1
|
|
|
+ * @param byte_2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static byte[] byteMerger(List<byte[]> list)
|
|
|
+ {
|
|
|
+ int length = 0;
|
|
|
+ for (int i = 0; i < list.size(); i++)
|
|
|
+ {
|
|
|
+ length += list.get(i).length;
|
|
|
+ }
|
|
|
+ byte[] rs = new byte[length];
|
|
|
+
|
|
|
+ int temp = 0;
|
|
|
+ for (int i = 0; i < list.size(); i++)
|
|
|
+ {
|
|
|
+ System.arraycopy(list.get(i), 0, rs, temp, list.get(i).length);
|
|
|
+ temp += list.get(i).length;
|
|
|
+ }
|
|
|
+ return rs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字节数组转成MAC地址
|
|
|
+ *
|
|
|
+ * @param macBytes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String byteToMac(byte[] macBytes)
|
|
|
+ {
|
|
|
+ String mac = "";
|
|
|
+ for (int i = 0; i < macBytes.length; i++)
|
|
|
+ {
|
|
|
+ String sTemp = ByteUtil.byteArrayToInt(new byte[] {macBytes[i]}, 1) + "";
|
|
|
+ mac = mac + sTemp + ":";
|
|
|
+ }
|
|
|
+
|
|
|
+ mac = mac.substring(0, mac.lastIndexOf(":"));
|
|
|
+ return mac;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] macToByte(String mac)
|
|
|
+ {
|
|
|
+ String[] macStr = mac.split(":");
|
|
|
+ byte[] macData = new byte[macStr.length];
|
|
|
+ for (int i = 0; i < macStr.length; i++)
|
|
|
+ {
|
|
|
+ macData[i] = (byte)Integer.parseInt(macStr[i]);
|
|
|
+ }
|
|
|
+ return macData;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字节数组转成IP地址
|
|
|
+ *
|
|
|
+ * @param ipBytes
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String byteToIp(byte[] ipBytes)
|
|
|
+ {
|
|
|
+ String ip = "";
|
|
|
+ for (int i = 0; i < ipBytes.length; i++)
|
|
|
+ {
|
|
|
+ ip = ip + ByteUtil.byteArrayToInt(new byte[] {ipBytes[i]}, 1) + ".";
|
|
|
+ }
|
|
|
+
|
|
|
+ ip = ip.substring(0, ip.lastIndexOf("."));
|
|
|
+ return ip;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] hexStringToBytes(String hexString)
|
|
|
+ {
|
|
|
+ if (hexString == null || hexString.equals(""))
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ hexString = hexString.toUpperCase();
|
|
|
+ int length = hexString.length() / 2;
|
|
|
+ char[] hexChars = hexString.toCharArray();
|
|
|
+ byte[] d = new byte[length];
|
|
|
+ for (int i = 0; i < length; i++)
|
|
|
+ {
|
|
|
+ int pos = i * 2;
|
|
|
+ d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
|
|
|
+ }
|
|
|
+ return d;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte charToByte(char c)
|
|
|
+ {
|
|
|
+ return (byte)"0123456789ABCDEF".indexOf(c);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args)
|
|
|
+ {
|
|
|
+ /* byte[] b = hexStringToBytes("570D260000FFFF000000000003034350535A1200000000000000000009006800180500E50054");
|
|
|
+
|
|
|
+ ZprotoTypeConfig config = new ZprotoTypeConfig();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ JavaStruct.unpack(config, b, ByteOrder.BIG_ENDIAN);
|
|
|
+
|
|
|
+ System.out.println(config.getFreq());
|
|
|
+ }
|
|
|
+ catch (StructException e)
|
|
|
+ {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ */
|
|
|
+ //System.out.println(Arrays.toString(b));
|
|
|
+ }
|
|
|
+}
|