liurunyu
2024-05-13 2b2a90e952c2c59642c41d8af5759ceb51b9e099
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java
@@ -33,9 +33,8 @@
    * @param index 下标位
    * @param len 长度
    * @return 返回 返回
    * @throws Exception 异常
    */
   public static boolean bytesIsAll0xFF(byte[] bs, int index, int len)throws Exception {
   public static boolean bytesIsAll0xFF(byte[] bs, int index, int len){
      int count = 0 ;
      for(int i = index; i < index + len; i++){
         if(bs[i] == (byte)0xFF){
@@ -47,26 +46,11 @@
   /**
    * 二进制转十进制数
    * @param str
    * @param str 二进制字符串
    * @return 返回
    * @throws Exception 异常
    */
   public static int binary2Int(String str) throws Exception {
        int cnt=0;
        int sum=0;
        str=new StringBuilder(str).reverse().toString();//反转字符串
        for(int i=0;i<str.length();i++){
            cnt++;
            if (str.charAt(i)=='1'){
                int mul=1;
                for (int j=1;j<cnt;j++){
                    mul*=2;
                }
                sum+=mul;
            }
            else continue;
        }
        return sum;
   public static int binary2Int(String str) {
      return Integer.parseInt(str, 2);
    }
@@ -74,26 +58,18 @@
    * 字节转存二进制
    *
    * @param b byte
    * @throws Exception 异常
    * @return 返回 String
    */
   public static String byte2Binary(byte b) throws Exception {
      int n = (b + 256) % 256 + 256;
      try {
         return Integer.toBinaryString(n).substring(1);
      } catch (Exception e) {
         throw new Exception("字节转换成二进制的字符串出错!", null);
      }
   public static String byte2Binary(byte b) {
      return Integer.toBinaryString(b & 0xFF) ;
   }
   /**
    * 字节转存8位二进制
    *
    * @param b
    *            byte
    * @throws Exception 异常
    * @param b  byte
    * @return 返回 String
    */
   public static String byte2bit8Binary(byte b) throws Exception {
   public static String byte2bit8Binary(byte b) {
      String s = byte2Binary(b);
      int len = s.length();
      for (int i = 0; i < 8 - len; i++) {
@@ -103,13 +79,11 @@
   }
   /**
    * 字节取bit
    * @param b
    * @param b
    * @throws Exception 异常
    * 字节数 取出8个bit
    * @param b 字节数
    * @return 返回 String
    */
   public static byte[] getBit(byte b) throws Exception {
   public static byte[] getBit(byte b) {
      byte[] bs = new byte[8] ;
      bs[0] = (byte)(b & 1) ;
      bs[1] = (byte)((b & 2) >> 1) ;
@@ -123,8 +97,8 @@
   }
   /**
    * 字节取bit
    * @param b
    * 字节数 取出bit
    * @param b 字节数
    * @param index 下标位
    * @throws Exception 异常
    * @return 返回 String
@@ -178,8 +152,7 @@
   public static void double2Bytes_BE(byte[] bs, double value, int from)throws Exception {
      boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
      if (b) {
         Long l = Double.doubleToLongBits(value);
         long2Bytes_BE(bs, l, from);
         long2Bytes_BE(bs, Double.doubleToLongBits(value), from);
      } else {
         throw new Exception("double2Bytes时数组越界");
      }
@@ -195,8 +168,7 @@
   public static void double2Bytes_LE(byte[] bs, double value, int from)throws Exception {
      boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
      if (b) {
         Long l = Double.doubleToLongBits(value);
         long2Bytes_LE(bs, l, from);
         long2Bytes_LE(bs, Double.doubleToLongBits(value), from);
      } else {
         throw new Exception("double2Bytes时数组越界");
      }
@@ -452,7 +424,7 @@
      boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
      if (b) {
         long s = 0;
         long s0 = bs[from + 0] ;// 最低位
         long s0 = bs[from] ;// 最低位
         long s1 = bs[from + 1] ;
         long s2 = bs[from + 2] ;
         long s3 = bs[from + 3] ;
@@ -1223,7 +1195,7 @@
   }
   /**
    * 大端模式《数据低位在数组高字节》
    * 小端模式《数据低位在数组高字节》
    * 字符串转换成byte数组
    * @value bs byte[]
    * @value str String
@@ -1501,5 +1473,5 @@
      System.out.println(v);
   }
   */
}