| | |
| | | * @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时数组越界"); |
| | | } |
| | |
| | | long l = 0; |
| | | long[] ls = new long[len] ; |
| | | for(int i = 0 ; i < len; i++){ |
| | | ls[i] = bs[i] ; |
| | | ls[i] = bs[i] & 0xFF ; |
| | | } |
| | | for(int i = len-1 ; i >= 0; i--){ |
| | | ls[(len - 1) - i] <<= 8 * i ; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | long s = 0; |
| | | long s0 = bs[from + 0] ;// 最低位 |
| | | long s1 = bs[from + 1] ; |
| | | long s2 = bs[from + 2] ; |
| | | long s3 = bs[from + 3] ; |
| | | long s4 = bs[from + 4] ; |
| | | long s5 = bs[from + 5] ; |
| | | long s6 = bs[from + 6] ; |
| | | long s7 = bs[from + 7] ; |
| | | long s0 = bs[from + 0] & 0xFF ;// 最低位 |
| | | long s1 = bs[from + 1] & 0xFF ; |
| | | long s2 = bs[from + 2] & 0xFF ; |
| | | long s3 = bs[from + 3] & 0xFF ; |
| | | long s4 = bs[from + 4] & 0xFF ; |
| | | long s5 = bs[from + 5] & 0xFF ; |
| | | long s6 = bs[from + 6] & 0xFF ; |
| | | long s7 = bs[from + 7] & 0xFF ; |
| | | |
| | | // s7不变 |
| | | s6 <<= 8; |
| | |
| | | short len = (short)(end - from + 1) ; |
| | | long[] ls = new long[len] ; |
| | | for(short i = 0 ; i < len; i++){ |
| | | ls[i] = bs[from + i] ; |
| | | ls[i] = bs[from + i] & 0xFF ; |
| | | } |
| | | for(short i = (short)(len-1) ; i >= 0; i--){ |
| | | ls[i] <<= 8 * (len - (i + 1)) ; |
| | |
| | | long l = 0; |
| | | long[] ls = new long[len] ; |
| | | for(int i = 0 ; i < len; i++){ |
| | | ls[i] = bs[i] ; |
| | | ls[i] = bs[i] & 0xFF ; |
| | | } |
| | | for(int i = 0 ; i < len; i++){ |
| | | ls[(len - 1) - i] <<= 8 * i ; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | long s = 0; |
| | | long s0 = bs[from + 0] ;// 最低位 |
| | | long s1 = bs[from + 1] ; |
| | | long s2 = bs[from + 2] ; |
| | | long s3 = bs[from + 3] ; |
| | | long s4 = bs[from + 4] ; |
| | | long s5 = bs[from + 5] ; |
| | | long s6 = bs[from + 6] ; |
| | | long s7 = bs[from + 7] ; |
| | | long s0 = bs[from] & 0xFF ;// 最低位 |
| | | long s1 = bs[from + 1] & 0xFF ; |
| | | long s2 = bs[from + 2] & 0xFF ; |
| | | long s3 = bs[from + 3] & 0xFF ; |
| | | long s4 = bs[from + 4] & 0xFF ; |
| | | long s5 = bs[from + 5] & 0xFF ; |
| | | long s6 = bs[from + 6] & 0xFF ; |
| | | long s7 = bs[from + 7] & 0xFF ; |
| | | |
| | | // s0不变 |
| | | s1 <<= 8; |
| | |
| | | short len = (short)(end - from + 1) ; |
| | | long[] ls = new long[len] ; |
| | | for(short i = 0 ; i < len; i++){ |
| | | ls[i] = bs[from + i] ; |
| | | ls[i] = bs[from + i] & 0xFF ; |
| | | } |
| | | for(short i = 0 ; i < len; i++){ |
| | | ls[i] <<= 8 * i ; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); |
| | | if (b) { |
| | | int s = 0; |
| | | int s0 = bs[from + 0] ;// 最低位 |
| | | int s1 = bs[from + 1] ; |
| | | int s2 = bs[from + 2] ; |
| | | int s3 = bs[from + 3] ; |
| | | int s0 = bs[from + 0] & 0xFF ;// 最低位 |
| | | int s1 = bs[from + 1] & 0xFF ; |
| | | int s2 = bs[from + 2] & 0xFF ; |
| | | int s3 = bs[from + 3] & 0xFF ; |
| | | |
| | | // s3不变 |
| | | s2 <<= 8; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); |
| | | if (b) { |
| | | int s = 0; |
| | | int s0 = bs[from + 0] ;// 最低位 |
| | | int s1 = bs[from + 1] ; |
| | | int s2 = bs[from + 2] ; |
| | | int s3 = bs[from + 3] ; |
| | | int s0 = bs[from + 0] & 0xFF ;// 最低位 |
| | | int s1 = bs[from + 1] & 0xFF ; |
| | | int s2 = bs[from + 2] & 0xFF ; |
| | | int s3 = bs[from + 3] & 0xFF ; |
| | | |
| | | // s0不变 |
| | | s1 <<= 8; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); |
| | | if (b) { |
| | | int s = 0; |
| | | int s0 = bs[from + 0] ; |
| | | int s1 = bs[from + 1] ; |
| | | int s0 = bs[from + 0] & 0xFF ; |
| | | int s1 = bs[from + 1] & 0xFF ; |
| | | |
| | | // s1不变 |
| | | s0 <<= 8; |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); |
| | | if (b) { |
| | | int s = 0; |
| | | int s0 = bs[from + 0] ; |
| | | int s1 = bs[from + 1] ; |
| | | int s0 = bs[from + 0] & 0xFF ; |
| | | int s1 = bs[from + 1] & 0xFF ; |
| | | |
| | | // s0不变 |
| | | s1 <<= 8; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 小端模式《数据低位在数组高字节》 |
| | | * 字符串转换成byte数组 |
| | | * @value bs byte[] |
| | | * @value str String |
| | |
| | | System.out.println(v); |
| | | } |
| | | */ |
| | | |
| | | |
| | | } |