From b35eadd37354ba8b2c1d17d5a744eef5164ba5ad Mon Sep 17 00:00:00 2001 From: Administrator <zhubaomin> Date: 星期五, 29 十二月 2023 14:08:07 +0800 Subject: [PATCH] Merge branch 'master' of http://8.140.179.55:20000/r/pipIrr-SV --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java | 92 +++++++++++++++++++++++---------------------- 1 files changed, 47 insertions(+), 45 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java index c782aa6..7fe22e9 100644 --- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java +++ b/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 @@ } /** - * 瀛楄妭鍙朾it - * @param b - * @param b - * @throws Exception 寮傚父 + * 瀛楄妭鏁� 鍙栧嚭8涓猙it + * @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 @@ } /** - * 瀛楄妭鍙朾it - * @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] ; @@ -1027,6 +999,34 @@ } return new String(buf); } + /** + * 灏哹yte[]杞崲涓�16杩涘埗瀛楃涓� + * + * @param bytes 寰呰浆鎹yte[] + * @return 杩斿洖 杞崲鍚庣殑瀛楃涓� + */ + public static String bytesToHex(byte[] bytes, int startIndex, int endIndex) { + byte[] bs = new byte[endIndex - startIndex + 1] ; + byte j = 0 ; + for(int i = startIndex; i <= endIndex; i++){ + bs[j++] = bytes[i] ; + } + //涓�涓猙yte涓�8浣嶏紝鍙敤涓や釜鍗佸叚杩涘埗浣嶆爣璇� + char[] buf = new char[bs.length * 2]; + int a = 0; + int index = 0; + for (byte b : bs) { // 浣跨敤闄や笌鍙栦綑杩涜杞崲 + if (b < 0) { + a = 256 + b; + } else { + a = b; + } + + buf[index++] = HEX_CHAR[a / 16]; + buf[index++] = HEX_CHAR[a % 16]; + } + return new String(buf); + } /** * 灏�16杩涘埗瀛楃涓茶浆鎹负byte[] @@ -1458,6 +1458,7 @@ // System.out.println(v5); // // } + /* public static void main(String[] args) throws Exception { byte[] bs = new byte[]{0x38, 0x36, 0x39, 0x31} ; String s = bytes2String_BE(bs, 0, 3) ; @@ -1471,5 +1472,6 @@ } System.out.println(v); } - + */ + } -- Gitblit v1.8.0