From 2b2a90e952c2c59642c41d8af5759ceb51b9e099 Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期一, 13 五月 2024 16:19:14 +0800 Subject: [PATCH] 根据王江海修改完善的协议(20240510),进行协议实现完善。 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java | 144 +++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 117 insertions(+), 27 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java index eda37b9..f66c2ad 100644 --- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java +++ b/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鏃舵暟缁勮秺鐣�"); } } + + + /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 涓庢柟娉昩ytes2Int绠楁硶涓�鏍凤紝鍙槸鎶婇『搴忓弽杩囨潵 + * @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鏃舵暟缁勮秺鐣�"); + } + } + /** + * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘�� + * 鏃犵鍙穝hort绫诲瀷杞崲鎴�2浣峛yte鏁扮粍 + * java娌℃湁鏃犵鍙风煭鏁村瀷鏁版嵁锛屽彧鏈夋湁绗﹀彿鐭暣鏁帮紝鍙栧�艰寖鍥存槸-32768~32767 + * 鑻ユā鎷熸棤绗﹀彿鐭暣鍨嬫暟鎹紝鍙栧�艰寖鍥存槸0鍒�65535 锛屽凡缁忚秴鍑轰簡java鐨勬湁绗﹀彿鏁存暟涓婇檺锛屾墍浠ュ彧鑳界敤java鐨処nt鍨嬭〃绀烘棤绗﹀彿鏁存暟 + * @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鏃舵暟缁勮秺鐣�"); +// } +// } + /** * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘�� * 鏃犵鍙穝hort绫诲瀷杞崲鎴�2浣峛yte鏁扮粍 @@ -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); +// +// +// } } -- Gitblit v1.8.0