liurunyu
2024-05-13 2b2a90e952c2c59642c41d8af5759ceb51b9e099
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java
@@ -1,6 +1,5 @@
package com.dy.common.util;
@SuppressWarnings("unuseed")
public class ByteUtilUnsigned {
   /**
    * 大端模式《数据低位在数组高字节》
@@ -36,7 +35,7 @@
         throw new Exception("int2Bytes时数组越界");
      }
   }
   /**
    * 小端模式《数据低位在数组低字节》
@@ -167,6 +166,90 @@
         throw new Exception("byte2Int时数组越界");
      }
   }
   /**
    * 小端模式《数据低位在数组低字节》
    * 与方法bytes2Int算法一样,只是把顺序反过来
    * @param bs
    * @return
    */
   public static long bytes2Long_LE(byte[] bs, int startIndex, int endIndex) throws Exception {
      boolean b = isOutOfArrLength(bs.length, endIndex);
      if (!b) {
         byte count = 0 ;
         long s = 0;
         long temp = 0 ;
         for(int i = startIndex ; i <= endIndex; i++){
            temp =  bs[i] & 0xFF ;// 数据的最低位在低字节
            if(temp < 0){
               temp = Byte.MAX_VALUE * 2 + 1 + temp + 1 ;
            }
            if(count > 0){
               temp <<= count * 8;
            }
            count++ ;
            s = s | temp ;
         }
         return s;
      } else {
         throw new Exception("bytes2Long_LE时数组越界");
      }
   }
   /**
    * 大端模式《数据低位在数组高字节》
    * 无符号short类型转换成2位byte数组
    * java没有无符号短整型数据,只有有符号短整数,取值范围是-32768~32767
    * 若模拟无符号短整型数据,取值范围是0到65535 ,已经超出了java的有符号整数上限,所以只能用java的Int型表示无符号整数
    * @value bs byte[]
    */
   public static long bytes2Long_BE(byte[] bs, int startIndex, int endIndex) throws Exception {
      boolean b = isOutOfArrLength(bs.length, endIndex);
      if (!b) {
         int count = endIndex - startIndex  ;
         long s = 0;
         long temp = 0 ;
         for(int i = startIndex ; i <= endIndex ; i++){
            temp =  bs[i] & 0xFF ;// 数据的最低位在低字节
            if(temp < 0){
               temp = Byte.MAX_VALUE * 2 + 1 + temp + 1 ;
            }
            if(count > 0){
               temp <<= count * 8;
            }
            count-- ;
            s = s | temp ;
         }
         return s;
      } else {
         throw new Exception("bytes2Long_LE时数组越界");
      }
   }
//   public static long bytes2Int_BE(byte[] bs, int from) throws Exception {
//      boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
//      if (!b) {
//         long s = 0;
//         long s0 = bs[from + 0] & 0xFF ;// 数据的最高位在低字节
//         long s1 = bs[from + 1] & 0xFF ;
//         long s2 = bs[from + 2] & 0xFF ;
//         long s3 = bs[from + 3] & 0xFF ;
//
//         // 最低位S3不变
//         s0 <<= 24;
//         s1 <<= 16;
//         s2 <<= 8;
//         s = s0 | s1 | s2 | s3;
//         if(s < 0){
//            //s = Integer.MAX_VALUE -s ;
//            s = Integer.MAX_VALUE * 2 + 1 + s + 1 ;
//         }
//         return s;
//      } else {
//         throw new Exception("byte2Int时数组越界");
//      }
//   }
   /**
    * 大端模式《数据低位在数组高字节》
    * 无符号short类型转换成2位byte数组
@@ -280,7 +363,7 @@
         throw new Exception("bytes2Short时数组越界");
      }
   }
   /**
    * 1位字节数组转换为短整型
@@ -292,7 +375,7 @@
      if (bs.length - 1 < index) {
         throw new Exception("byte2Short(byte[] bs, int index)时数组越界");
      } else {
         byte bv = (byte)(bs[index] & 0xff) ;
         byte bv = (byte)(bs[index] & 0xff) ;
         short s = 0 ;
         if(bv < 0){
            s = (short)(Byte.MAX_VALUE * 2 + 1 + bv + 1) ;
@@ -322,7 +405,7 @@
         bs[index] = Integer.valueOf(value & 0xff).byteValue() ;
      }
   }
   /**
    * 判断所有字节是否为0xFF
    * @param bs  字节数组
@@ -343,7 +426,7 @@
   /**
    * 判断数组下标是否越界
    *
    *
    * @value bsLength 数组总长度
    * @value toSite 数组偏移量
    * @return 结果
@@ -355,27 +438,34 @@
         return true;
      }
   }
   /*
   public static void main(String[] args) throws Exception{
//      int d = 123456;
//      byte[] bs = new byte[4] ;
//      int2Bytes_BE(bs, d, 0) ;
//      System.out.println(ByteUtil.bytes2Hex(bs, false));
//      long dd = bytes2Int_BE(bs, 0) ;
//      System.out.println(dd);
//
//      byte[] bb = new byte[1] ;
//      bb[0] = (byte)255 ;
//      short s = byte2Byte(bb, 0);
//      System.out.println(s);
      byte[] bs = new byte[]{(byte)0x00, (byte)0x00, (byte)0x3A, (byte)0x88} ;
      //byte[] bs = new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff} ;
      boolean flag = ByteUtilUnsigned.bytesIsAll0xFF(bs, 0, 4) ;
      System.out.println(flag);
      Long s = ByteUtilUnsigned.bytes2Int_BE(bs, 0);
      System.out.println(s);
      byte[] bs = new byte[]{(byte)0x38,(byte)0x1d,(byte)0x00,(byte)0x00,(byte)0x00} ;
      Long lg1 = ByteUtilUnsigned.bytes2Long_LE(bs, 0, 4) ;
      Long lg2 = ByteUtilUnsigned.bytes2Long_BE(bs, 0, 4) ;
      System.out.println(lg1);
      System.out.println(lg2);
   }
   */
//   public static void main(String[] args) throws Exception{
////      int d = 123456;
////      byte[] bs = new byte[4] ;
////      int2Bytes_BE(bs, d, 0) ;
////      System.out.println(ByteUtil.bytes2Hex(bs, false));
////      long dd = bytes2Int_BE(bs, 0) ;
////      System.out.println(dd);
////
////      byte[] bb = new byte[1] ;
////      bb[0] = (byte)255 ;
////      short s = byte2Byte(bb, 0);
////      System.out.println(s);
//
//      byte[] bs = new byte[]{(byte)0x00, (byte)0x00, (byte)0x3A, (byte)0x88} ;
//      //byte[] bs = new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff} ;
//      boolean flag = ByteUtilUnsigned.bytesIsAll0xFF(bs, 0, 4) ;
//      System.out.println(flag);
//      Long s = ByteUtilUnsigned.bytes2Int_BE(bs, 0);
//      System.out.println(s);
//
//
//   }
}