| | |
| | | * @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){ |
| | |
| | | |
| | | /** |
| | | * 二进制转十进制数 |
| | | * @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); |
| | | } |
| | | |
| | | |
| | |
| | | * 字节转存二进制 |
| | | * |
| | | * @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++) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 字节取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) ; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 字节取bit |
| | | * @param b |
| | | * 字节数 取出bit |
| | | * @param b 字节数 |
| | | * @param index 下标位 |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | |
| | | 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时数组越界"); |
| | | } |
| | |
| | | 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时数组越界"); |
| | | } |
| | |
| | | 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] ; |
| | |
| | | System.out.println(v); |
| | | } |
| | | */ |
| | | |
| | | |
| | | } |