jiazx0107@163.com
2024-01-04 013c5f28422defd9b47abbe17a827be693872a26
src/main/java/com/fzzy/api/utils/BytesUtil.java
@@ -4,461 +4,469 @@
public class BytesUtil {
   /**
    * bytes输出十进制
    *
    * @param bytes
    * @return
    */
   public static Integer bytesToInt(byte[] bytes) {
      String str = "";
      int i = 0;
      boolean flag = true && bytes.length > 0;
      while (flag) {
         Byte b = bytes[i];
         i++;
         Integer bi = Byte.toUnsignedInt(b);
         str += toHexString(bi);
         if (i >= bytes.length || str.endsWith("EEEE"))
            flag = false;
      }
      return Integer.parseInt(str, 16);
   }
    /**
     * bytes输出十进制
     *
     * @param bytes
     * @return
     */
    public static Integer bytesToInt(byte[] bytes) {
        String str = "";
        int i = 0;
        boolean flag = true && bytes.length > 0;
        while (flag) {
            Byte b = bytes[i];
            i++;
            Integer bi = Byte.toUnsignedInt(b);
            str += toHexString(bi);
            if (i >= bytes.length || str.endsWith("EEEE"))
                flag = false;
        }
        return Integer.parseInt(str, 16);
    }
   /**
    * bytes输出十六进制字符串
    *
    * @param bytes
    * @return
    */
   public static String bytesToString(byte[] bytes) {
      String str = "";
      int i = 0;
      boolean flag = true && bytes.length > 0;
      while (flag) {
         Byte b = bytes[i];
         i++;
         Integer bi = Byte.toUnsignedInt(b);
         str += toHexString(bi);
         if (i >= bytes.length)
            flag = false;
      }
      return str;
   }
    /**
     * bytes输出十六进制字符串
     *
     * @param bytes
     * @return
     */
    public static String bytesToString(byte[] bytes) {
        String str = "";
        int i = 0;
        boolean flag = true && bytes.length > 0;
        while (flag) {
            Byte b = bytes[i];
            i++;
            Integer bi = Byte.toUnsignedInt(b);
            str += toHexString(bi);
            if (i >= bytes.length)
                flag = false;
        }
        return str;
    }
   /**
    * 十六进制转字节
    *
    * @param hex
    * @return
    */
   public static byte hexToByte(Integer hex) {
      return hex.byteValue();
   }
    /**
     * 十六进制转字节
     *
     * @param hex
     * @return
     */
    public static byte hexToByte(Integer hex) {
        return hex.byteValue();
    }
   /**
    * short转2字节 已调整高低位
    *
    * @param number
    * @return
    */
   public static byte[] shortToByte(short number) {
      int temp = number;
      byte[] b = new byte[2];
      for (int i = 0; i < b.length; i++) {
         b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
         temp = temp >> 8; // 向右移8位
      }
      return b;
   }
    /**
     * short转2字节 已调整高低位
     *
     * @param number
     * @return
     */
    public static byte[] shortToByte(short number) {
        int temp = number;
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }
   /**
    * 二进制子串转16进制子串
    *
    * @param bin
    * @return
    */
   public static String binToHex(String bin) {
      String hexStr = "";
      int size = bin.length() / 8;
      for (int i = 0; i < size; i++) {
         String value = bin.substring(i * 8, (i + 1) * 8);
         int temp = Integer.parseInt(value, 2);
         String tempHex = Integer.toHexString(temp);
         hexStr += tempHex;
      }
      return hexStr;
   }
    /**
     * 二进制子串转16进制子串
     *
     * @param bin
     * @return
     */
    public static String binToHex(String bin) {
        String hexStr = "";
        int size = bin.length() / 8;
        for (int i = 0; i < size; i++) {
            String value = bin.substring(i * 8, (i + 1) * 8);
            int temp = Integer.parseInt(value, 2);
            String tempHex = Integer.toHexString(temp);
            hexStr += tempHex;
        }
        return hexStr;
    }
   /**
    * 二进制转16进制,不足2位补位
    *
    * @param bin
    * @return
    */
   public static String bin2Hex(String bin) {
      String hexStr = "";
      int size = bin.length() / 8;
      for (int i = 0; i < size; i++) {
         String value = bin.substring(i * 8, (i + 1) * 8);
         int temp = Integer.parseInt(value, 2);
         String tempHex = Integer.toHexString(temp);
         if (tempHex.length() < 2) {
            tempHex = "0" + tempHex;
         }
         hexStr += tempHex;
      }
    /**
     * 二进制转16进制,不足2位补位
     *
     * @param bin
     * @return
     */
    public static String bin2Hex(String bin) {
        String hexStr = "";
        int size = bin.length() / 8;
        for (int i = 0; i < size; i++) {
            String value = bin.substring(i * 8, (i + 1) * 8);
            int temp = Integer.parseInt(value, 2);
            String tempHex = Integer.toHexString(temp);
            if (tempHex.length() < 2) {
                tempHex = "0" + tempHex;
            }
            hexStr += tempHex;
        }
      return hexStr;
   }
        return hexStr;
    }
   /**
    * 转为十六进制串,不足2位补0
    *
    * @param value
    * @return
    */
   public static String toHexString(int value) {
      String tempHex = Integer.toHexString(value);
      if (tempHex.length() < 2) {
         tempHex = "0" + tempHex;
      }
      return tempHex.toUpperCase();
   }
    /**
     * 转为十六进制串,不足2位补0
     *
     * @param value
     * @return
     */
    public static String toHexString(int value) {
        String tempHex = Integer.toHexString(value);
        if (tempHex.length() < 2) {
            tempHex = "0" + tempHex;
        }
        return tempHex.toUpperCase();
    }
   public static String getTargetId(String value, boolean isTwo) {
      String tempHex = Integer.toHexString(Integer.valueOf(value));
      if (tempHex.length() < 2) {
         tempHex = "0" + tempHex;
      }
      if (isTwo) {
         if (tempHex.length() < 4) {
            tempHex = "0" + tempHex;
         }
         if (tempHex.length() < 4) {
            tempHex = "0" + tempHex;
         }
      }
      return tempHex.toUpperCase();
   }
    public static String getTargetId(String value, boolean isTwo) {
        String tempHex = Integer.toHexString(Integer.valueOf(value));
        if (tempHex.length() < 2) {
            tempHex = "0" + tempHex;
        }
        if (isTwo) {
            if (tempHex.length() < 4) {
                tempHex = "0" + tempHex;
            }
            if (tempHex.length() < 4) {
                tempHex = "0" + tempHex;
            }
        }
        return tempHex.toUpperCase();
    }
   /**
    * 根据数值获取到长度为2字节的16进制字符
    *
    * @param value
    * @return
    */
   public static String getHex2LenStr(int value) {
      String tempHex = Integer.toHexString(value);
      if (tempHex.length() < 2) {
         tempHex = "0" + tempHex;
      }
      if (tempHex.length() < 4) {
         tempHex = "0" + tempHex;
      }
      if (tempHex.length() < 4) {
         tempHex = "0" + tempHex;
      }
      return tempHex.toUpperCase();
   }
    /**
     * 根据数值获取到长度为2字节的16进制字符
     *
     * @param value
     * @return
     */
    public static String getHex2LenStr(int value) {
        String tempHex = Integer.toHexString(value);
        if (tempHex.length() < 2) {
            tempHex = "0" + tempHex;
        }
        if (tempHex.length() < 4) {
            tempHex = "0" + tempHex;
        }
        if (tempHex.length() < 4) {
            tempHex = "0" + tempHex;
        }
        return tempHex.toUpperCase();
    }
   public static String getOrderId(int value) {
      return getHex2LenStr(value);
   }
    public static String getOrderId(int value) {
        return getHex2LenStr(value);
    }
   /**
    * 将value转为same个相同满8位的二进制字串
    *
    * @param value
    * @param same
    * @return
    */
   public static String toBinary8StringSame(int value, int same) {
      String tempBinStr = toBinary8String(value);
      String rsBinStr = "";
      for (int i = 0; i < same; i++) {
         rsBinStr += tempBinStr;
      }
      return rsBinStr;
   }
    /**
     * 将value转为same个相同满8位的二进制字串
     *
     * @param value
     * @param same
     * @return
     */
    public static String toBinary8StringSame(int value, int same) {
        String tempBinStr = toBinary8String(value);
        String rsBinStr = "";
        for (int i = 0; i < same; i++) {
            rsBinStr += tempBinStr;
        }
        return rsBinStr;
    }
   /**
    * 不足width个字节宽度时,前面补0至width*8
    *
    * @param value
    * @param width
    * @return
    */
   public static String toBinary8String(int value, int width) {
      String tempBinStr = toBinary8String(value);
      int size = tempBinStr.length();
      for (int i = 0; i < width; i++) {
         int temp = (i + 1) * 8;
         if (temp > size) {
            tempBinStr = "00000000" + tempBinStr;
            size = tempBinStr.length();
         }
      }
      return tempBinStr;
   }
    /**
     * 不足width个字节宽度时,前面补0至width*8
     *
     * @param value
     * @param width
     * @return
     */
    public static String toBinary8String(int value, int width) {
        String tempBinStr = toBinary8String(value);
        int size = tempBinStr.length();
        for (int i = 0; i < width; i++) {
            int temp = (i + 1) * 8;
            if (temp > size) {
                tempBinStr = "00000000" + tempBinStr;
                size = tempBinStr.length();
            }
        }
        return tempBinStr;
    }
   /**
    * 补足8位
    *
    * @param value
    * @return
    */
   public static String toBinary8String(int value) {
      String temp = Integer.toBinaryString(value);
      if (value == 0) {
         temp = "00000000";
      }
      int length = temp.length();
      while (length < 8) {
         temp = "0" + temp;
         length = temp.length();
      }
      return temp;
   }
    /**
     * 补足8位
     *
     * @param value
     * @return
     */
    public static String toBinary8String(int value) {
        String temp = Integer.toBinaryString(value);
        if (value == 0) {
            temp = "00000000";
        }
        int length = temp.length();
        while (length < 8) {
            temp = "0" + temp;
            length = temp.length();
        }
        return temp;
    }
   public static String toEmptyBinaryWidthString(int width) {
      String str = "";
      for (int i = 0; i < width; i++) {
         str += "00000000";
      }
      return str;
   }
    public static String toEmptyBinaryWidthString(int width) {
        String str = "";
        for (int i = 0; i < width; i++) {
            str += "00000000";
        }
        return str;
    }
   public static byte[] emptyBytes(int bytes) {
      byte[] bys = new byte[bytes];
      for (int i = 0; i < bytes; i++) {
         bys[i] = emptyByte();
      }
      return bys;
   }
    public static byte[] emptyBytes(int bytes) {
        byte[] bys = new byte[bytes];
        for (int i = 0; i < bytes; i++) {
            bys[i] = emptyByte();
        }
        return bys;
    }
   public static byte emptyByte() {
      Integer b0 = 0x00;
      return b0.byteValue();
   }
    public static byte emptyByte() {
        Integer b0 = 0x00;
        return b0.byteValue();
    }
   public static byte binToBytes(String bin) {
      return Integer.valueOf(bin, 2).byteValue();
   }
    public static byte binToBytes(String bin) {
        return Integer.valueOf(bin, 2).byteValue();
    }
   public static byte[] binaryStrToBytes(String binStr) {
      int size = binStr.length() / 8;
      byte[] bs = new byte[size];
      for (int i = 0; i < size; i++) {
         String temp = binStr.substring(i * 8, (i + 1) * 8);
         bs[i] = Integer.valueOf(temp, 2).byteValue();
      }
      return bs;
   }
    public static byte[] binaryStrToBytes(String binStr) {
        int size = binStr.length() / 8;
        byte[] bs = new byte[size];
        for (int i = 0; i < size; i++) {
            String temp = binStr.substring(i * 8, (i + 1) * 8);
            bs[i] = Integer.valueOf(temp, 2).byteValue();
        }
        return bs;
    }
   /**
    * 十六进制串转字节数组
    *
    * @param hexStr
    * @return
    */
   public static byte[] hexStrToBytes(String hexStr) {
      int size = hexStr.length() / 2;
      byte[] bytes = new byte[size];
      for (int i = 0; i < size; i++) {
         String tmp = hexStr.substring(i * 2, (i + 1) * 2);
         Integer tmpHex = Integer.parseInt(tmp, 16);
         bytes[i] = tmpHex.byteValue();
      }
      return bytes;
   }
    /**
     * 十六进制串转字节数组
     *
     * @param hexStr
     * @return
     */
    public static byte[] hexStrToBytes(String hexStr) {
        int size = hexStr.length() / 2;
        byte[] bytes = new byte[size];
        for (int i = 0; i < size; i++) {
            String tmp = hexStr.substring(i * 2, (i + 1) * 2);
            Integer tmpHex = Integer.parseInt(tmp, 16);
            bytes[i] = tmpHex.byteValue();
        }
        return bytes;
    }
   /**
    * 两字节高低位装换
    *
    * @param b
    * @return
    */
   public static byte[] hexHeightLow(byte[] b) {
      byte[] b2 = new byte[2];
      // 转换高低位
      b2[0] = b[1];
      b2[1] = b[0];
      return b2;
   }
    /**
     * 两字节高低位装换
     *
     * @param b
     * @return
     */
    public static byte[] hexHeightLow(byte[] b) {
        byte[] b2 = new byte[2];
        // 转换高低位
        b2[0] = b[1];
        b2[1] = b[0];
        return b2;
    }
   /**
    * 16进制转换为10进制
    *
    * @param strHex
    * @return
    */
   public static int hexToInt(String strHex) {
      short s = (short) (Integer.valueOf(strHex, 16) & 0xffff);
      return s;
   }
    /**
     * 16进制转换为10进制
     *
     * @param strHex
     * @return
     */
    public static int hexToInt(String strHex) {
        short s = (short) (Integer.valueOf(strHex, 16) & 0xffff);
        return s;
    }
   /**
    * 16进制转换为10进制
    *
    * @param strHex
    * @return
    */
   public static int hexToBigInt(String strHex) {
      BigInteger bigint = new BigInteger(strHex, 16);
      return bigint.intValue();
   }
    /**
     * 16进制转换为10进制
     *
     * @param strHex
     * @return
     */
    public static int hexToBigInt(String strHex) {
        BigInteger bigint = new BigInteger(strHex, 16);
        return bigint.intValue();
    }
   /**
    * int转16进制字符串 2位 00 00
    *
    * @param num
    * @return
    */
   public static String intToHexStr(int num) {
      // 需要使用2字节表示b
      return String.format("%04x", num).toUpperCase();
   }
    /**
     * int转16进制字符串 2位 00 00
     *
     * @param num
     * @return
     */
    public static String intToHexStr(int num) {
        // 需要使用2字节表示b
        return String.format("%04x", num).toUpperCase();
    }
   // 计算16进制对应的数值
   public static int GetHex(char ch) throws Exception {
      if (ch >= '0' && ch <= '9')
         return (int) (ch - '0');
      if (ch >= 'a' && ch <= 'f')
         return (int) (ch - 'a' + 10);
      if (ch >= 'A' && ch <= 'F')
         return (int) (ch - 'A' + 10);
      throw new Exception("error param");
   }
   // 计算幂
   public static int GetPower(int nValue, int nCount) throws Exception {
      if (nCount < 0)
         throw new Exception("nCount can't small than 1!");
      if (nCount == 0)
         return 1;
      int nSum = 1;
      for (int i = 0; i < nCount; ++i) {
         nSum = nSum * nValue;
      }
      return nSum;
   }
    public static void main(String[] args) {
   public static void main(String[] args) {
      double d1 = hexToBigInt("000906EB")/100.0;
      double d2 = hexToBigInt("0004F6E6")/100.0;
      System.out.println(d1);
      System.out.println(d2);
      System.out.println(d1 + d2);
   }
//      double d1 = hexToBigInt("000906EB")/100.0;
//      double d2 = hexToBigInt("0004F6E6")/100.0;
//
//      System.out.println(d1);
//      System.out.println(d2);
//
//      System.out.println(d1 + d2);
   /**
    * 16进制字符串 高低换位
    *
    * @param info
    * @return
    */
   public static String tran_LH(String info) {
      return info.substring(2) + info.substring(0, 2);
   }
   /**
    * 16进制字符串 高低换位 8个字符
    *
    * @param info
    * @return
    */
   public static String tran_LH8(String info) {
      return tran_LH(info.substring(4)) + tran_LH(info.substring(0, 4));
   }
        String kyeNumBin = "10100100";
        String key = "00000" + kyeNumBin.substring(0, 3);
   /**
    * 将 4字节的16进制字符串,转换为32位带符号的十进制浮点型
    *
    * 42c60000 -> 99.00
    *
    * @param str
    *            4字节 16进制字符
    * @return
    */
   public static float hexToFloat(String str) {
      return Float.intBitsToFloat(new BigInteger(str, 16).intValue());
   }
        System.out.println("-----key-----" + key);
   /**
    * 将带符号的32位浮点数装换为16进制
    *
    * 99.00 -> 42c60000
    *
    *
    * @param value
    * @return
    */
   public static String folatToHexString(Float value) {
      return Integer.toHexString(Float.floatToIntBits(value));
   }
        String bin2hex = BytesUtil.bin2Hex(key);
   /**
    * int转16进制字符串 1位置 00
    *
    * @param num
    * @return
    */
   public static String intToHexStr1(int num) {
      // 需要使用2字节表示b
      return String.format("%02x", num).toUpperCase();
   }
        System.out.println("-----bin2hex-----" + bin2hex);
   public static String hexString2binaryString(String hexString, int num) {
      //16进制转10进制
      BigInteger sint = new BigInteger(hexString, num);
      //10进制转2进制
      String str = sint.toString(2);
      if(str.length() < num){
         for (int i = str.length(); i < num; i++) {
            str = "0" + str;
         }
      }
      return str;
   }
        int keyValue = BytesUtil.hexToInt(bin2hex);
   /**
    * 将二进制转换为10进制
    * @param binStr
    * @return
    */
   public  static  Integer biannary2Decimal(String binStr){
        System.out.println("-----keyValue-----" + keyValue);
      Integer sum = 0;
      int len = binStr.length();
        key = "0000" + kyeNumBin.substring(4);
      for (int i=1;i<=len;i++){
         //第i位 的数字为:
         int dt = Integer.parseInt(binStr.substring(i-1,i));
         sum+=(int)Math.pow(2,len-i)*dt;
      }
      return  sum;
   }
        System.out.println("-----key-----" + key);
   /**
    * 二进制补码:取反口加1
    * @param str
    * @return
    */
   public static Integer twoToString(String str) {
      String two = "";
      String[] split = str.split("");
      for (int i = 0;i< split.length;i++){
         if("1".equals(split[i])){
            two += "0";
         }else if("0".equals(split[i])){
            two += "1";
         }
      }
      return biannary2Decimal(two) + 1;
   }
        bin2hex = BytesUtil.bin2Hex(key);
        System.out.println("-----bin2hex-----" + bin2hex);
        keyValue = BytesUtil.hexToInt(bin2hex);
        System.out.println("-----keyValue-----" + keyValue);
    }
    /**
     * 16进制字符串 高低换位
     *
     * @param info
     * @return
     */
    public static String tran_LH(String info) {
        return info.substring(2) + info.substring(0, 2);
    }
    /**
     * 16进制字符串 高低换位 8个字符
     *
     * @param info
     * @return
     */
    public static String tran_LH8(String info) {
        return tran_LH(info.substring(4)) + tran_LH(info.substring(0, 4));
    }
    /**
     * 将 4字节的16进制字符串,转换为32位带符号的十进制浮点型
     * <p>
     * 42c60000 -> 99.00
     *
     * @param str 4字节 16进制字符
     * @return
     */
    public static float hexToFloat(String str) {
        return Float.intBitsToFloat(new BigInteger(str, 16).intValue());
    }
    /**
     * 将带符号的32位浮点数装换为16进制
     * <p>
     * 99.00 -> 42c60000
     *
     * @param value
     * @return
     */
    public static String folatToHexString(Float value) {
        return Integer.toHexString(Float.floatToIntBits(value));
    }
    /**
     * int转16进制字符串 1位置 00
     *
     * @param num
     * @return
     */
    public static String intToHexStr1(int num) {
        // 需要使用2字节表示b
        return String.format("%02x", num).toUpperCase();
    }
    public static String hexString2binaryString(String hexString, int num) {
        //16进制转10进制
        BigInteger sint = new BigInteger(hexString, num);
        //10进制转2进制
        String str = sint.toString(2);
        if (str.length() < num) {
            for (int i = str.length(); i < num; i++) {
                str = "0" + str;
            }
        }
        return str;
    }
    /**
     * 将二进制转换为10进制
     *
     * @param binStr
     * @return
     */
    public static Integer biannary2Decimal(String binStr) {
        Integer sum = 0;
        int len = binStr.length();
        for (int i = 1; i <= len; i++) {
            //第i位 的数字为:
            int dt = Integer.parseInt(binStr.substring(i - 1, i));
            sum += (int) Math.pow(2, len - i) * dt;
        }
        return sum;
    }
    /**
     * 二进制补码:取反口加1
     *
     * @param str
     * @return
     */
    public static Integer twoToString(String str) {
        String two = "";
        String[] split = str.split("");
        for (int i = 0; i < split.length; i++) {
            if ("1".equals(split[i])) {
                two += "0";
            } else if ("0".equals(split[i])) {
                two += "1";
            }
        }
        return biannary2Decimal(two) + 1;
    }
    public static String byteToHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        return hex;
    }
}