From 1a2b07f01ba4616fd9e894dddf474b56d020158c Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期一, 07 四月 2025 15:18:51 +0800 Subject: [PATCH] 整理版本 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java | 474 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 474 insertions(+), 0 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 new file mode 100644 index 0000000..4a6e509 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtilUnsigned.java @@ -0,0 +1,474 @@ +package com.dy.common.util; + +public class ByteUtilUnsigned { + /** + * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘�� + * 鏃犵鍙穒nt绫诲瀷杞崲鎴�4浣峛yte鏁扮粍 + * java娌℃湁鏃犵鍙锋暣鍨嬫暟鎹紝鍙湁鏈夌鍙锋暣鏁帮紝鍙栧�艰寖鍥存槸-2147483648~2147483647 + * C鏈夋棤绗﹀彿鏁村瀷鏁版嵁锛屽彇鍊艰寖鍥存槸0鍒�4294967295锛屽凡缁忚秴鍑轰簡java鐨勬湁绗﹀彿鏁存暟涓婇檺锛屾墍浠ュ彧鑳界敤java鐨刲ong鍨嬭〃绀烘棤绗﹀彿鏁存暟 + * @value bs byte[] + * @value value int int绫诲瀷鐨勫弬鏁� + * @value from int + * @throws Exception 寮傚父 + */ + public static void int2Bytes_BE(byte[] bs, long value, int from)throws Exception { + int len = 4 ; + Long maxIntUnsigned = Long.valueOf(Integer.MAX_VALUE * 2 + 1) ; + Long minIntUnsigned = Long.valueOf(Integer.MIN_VALUE * 2) ; + if(value < minIntUnsigned || value > maxIntUnsigned ){ + throw new Exception("鏁版嵁" + value + "瓒呭嚭浜嗘棤绗﹀彿Int鍨嬬殑鍙栧�艰寖鍥�(" + minIntUnsigned + "~" + maxIntUnsigned + ")") ; + } + + int temp ; + if(value > Integer.MAX_VALUE){ + temp = (Long.valueOf(value - (Integer.MAX_VALUE * 2 + 1) - 1)).intValue() ; + }else{ + temp = Long.valueOf(value).intValue() ; + } + boolean b = isOutOfArrLength(bs.length, (from - 1) + len); + if (!b) { + for (int i = (len - 1); i >= 0; i--) { + bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//灏嗘渶浣庝綅淇濆瓨鍦ㄩ珮瀛楄妭 + temp = temp >> 8; // 鍚戝彸绉�8浣� + } + } else { + throw new Exception("int2Bytes鏃舵暟缁勮秺鐣�"); + } + } + + + /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 涓庢柟娉昳nt2Bytes绠楁硶涓�鏍凤紝鍙槸鎶婇『搴忓弽杩囨潵 + * @value bs byte[] + * @value value int int绫诲瀷鐨勫弬鏁� + * @value from int + */ + public static void int2Bytes_LE(byte[] bs, long value, int from)throws Exception { + int len = 4 ; + Long maxIntUnsigned = Long.valueOf(Integer.MAX_VALUE) * 2 + 1; + Long minIntUnsigned = Long.valueOf(Integer.MIN_VALUE) * 2 ; + if(value < minIntUnsigned || value > maxIntUnsigned ){ + throw new Exception("鏁版嵁" + value + "瓒呭嚭浜嗘棤绗﹀彿Int鍨嬬殑鍙栧�艰寖鍥�(" + minIntUnsigned + "~" + maxIntUnsigned + ")") ; + } + + int temp ; + if(value > Integer.MAX_VALUE){ + temp = (Long.valueOf(value - (Integer.MAX_VALUE * 2 + 1) - 1)).intValue() ; + }else{ + temp = Long.valueOf(value).intValue() ; + } + boolean b = isOutOfArrLength(bs.length, (from - 1) + len); + if (!b) { + for (int i = 0; i <= len ; i++) { + bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄤ綆瀛楄妭 + temp = temp >> 8; // 鍚戝彸绉�8浣� + } + } else { + throw new Exception("int2Bytes鏃舵暟缁勮秺鐣�"); + } + } + + /** + * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘�� + * 4浣嶅瓧鑺傛暟缁勮浆鎹负鏁村瀷 + * @param bs 瀛楄妭鏁扮粍 + * @param from 璧峰浣嶇疆 + * @return 缁撴灉 + */ + 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鏃舵暟缁勮秺鐣�"); + } + } + + /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 涓庢柟娉昩ytes2Int绠楁硶涓�鏍凤紝鍙槸鎶婇『搴忓弽杩囨潵 + * @param bs 瀛楄妭鏁扮粍 + * @param from 瀛楄妭鏁扮粍璧峰浣嶇疆 + * @return + */ + public static long bytes2Int_LE(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 ; + + // S0涓嶅彉 + s1 <<= 8; + s2 <<= 16; + s3 <<= 24; + 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鏃舵暟缁勮秺鐣�"); + } + } + + /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 涓庢柟娉昩ytes2Int绠楁硶涓�鏍凤紝鍙槸鎶婇『搴忓弽杩囨潵 + * @param bs 瀛楄妭鏁扮粍 + * @param from 瀛楄妭鏁扮粍璧峰浣嶇疆 + * @param has3Byte 鏄惁鍖呭惈鏁版嵁涓嬫爣涓�3鐨勫瓧鑺傦紝濡傛灉涓嶅寘鍚疄闄呭鐞嗙殑鏄�3瀛楄妭鐨勬暣鏁� + * @return + */ + public static long bytes2Int_LE(byte[] bs, int from, boolean has3Byte) 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 = 0L ; + if(!has3Byte){ + s3 = bs[from + 3] & 0xFF ; + } + + + // S0涓嶅彉 + s1 <<= 8; + s2 <<= 16; + s3 <<= 24; + 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鏃舵暟缁勮秺鐣�"); + } + } + + + /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 涓庢柟娉昩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鏁扮粍 + * java娌℃湁鏃犵鍙风煭鏁村瀷鏁版嵁锛屽彧鏈夋湁绗﹀彿鐭暣鏁帮紝鍙栧�艰寖鍥存槸-32768~32767 + * 鑻ユā鎷熸棤绗﹀彿鐭暣鍨嬫暟鎹紝鍙栧�艰寖鍥存槸0鍒�65535 锛屽凡缁忚秴鍑轰簡java鐨勬湁绗﹀彿鏁存暟涓婇檺锛屾墍浠ュ彧鑳界敤java鐨処nt鍨嬭〃绀烘棤绗﹀彿鏁存暟 + * @value bs byte[] + * @value value int int绫诲瀷鐨勫弬鏁� + * @value from int + */ + public static void short2Bytes_BE(byte[] bs, int value, int from)throws Exception { + int maxShortUnsigned = Integer.valueOf(Short.MAX_VALUE) * 2 + 1; + int minShortUnsigned = Integer.valueOf(Short.MIN_VALUE) * 2 ; + if(value < minShortUnsigned || value > maxShortUnsigned ){ + throw new Exception("鏁版嵁" + value + "瓒呭嚭浜嗘棤绗﹀彿short鍨嬬殑鍙栧�艰寖鍥�(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ; + } + short temp = 0 ; + if(value > Short.MAX_VALUE){ + temp = (Integer.valueOf(value - (Short.MAX_VALUE * 2 + 1) - 1)).shortValue() ;//(Integer.valueOf(Short.MAX_VALUE - value)).shortValue() ; + }else{ + temp = Integer.valueOf(value).shortValue() ; + } + boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); + if (!b) { + for (int i = 1; i >= 0; i--) { + bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//灏嗘渶浣庝綅淇濆瓨鍦ㄩ珮瀛楄妭 + temp = (short)(temp >> 8); // 鍚戝彸绉�8浣� + } + } else { + throw new Exception("short2Bytes鏃舵暟缁勮秺鐣�"); + } + } + /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 涓庢柟娉晄hort2Bytes绠楁硶涓�鏍凤紝鍙槸鎶婇『搴忓弽杩囨潵 + * @value bs byte[] + * @value value int int绫诲瀷鐨勫弬鏁� + * @value from int + */ + public static void short2Bytes_LE(byte[] bs, int value, int from)throws Exception { + int len = 2 ; + int maxShortUnsigned = Integer.valueOf(Short.MAX_VALUE) * 2 + 1; + int minShortUnsigned = Integer.valueOf(Short.MIN_VALUE) * 2 ; + if(value < minShortUnsigned || value > maxShortUnsigned ){ + throw new Exception("鏁版嵁" + value + "瓒呭嚭浜嗘棤绗﹀彿short鍨嬬殑鍙栧�艰寖鍥�(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ; + } + short temp = 0 ; + if(value > Short.MAX_VALUE){ + temp = (Integer.valueOf(value - (Short.MAX_VALUE * 2 + 1) - 1)).shortValue() ;//(Integer.valueOf(Short.MAX_VALUE - value)).shortValue() ; + }else{ + temp = Integer.valueOf(value).shortValue() ; + } + boolean b = isOutOfArrLength(bs.length, (from - 1) + len); + if (!b) { + for (int i = 0; i < len; i++) { + bs[from + i] = Integer.valueOf(temp & 0xff).byteValue();//灏嗘暟鎹綆浣嶄繚瀛樺湪鏁版嵁浣庡瓧鑺� + temp = (short)(temp >> 8); // 鍚戝彸绉�8浣� + } + } else { + throw new Exception("short2Bytes鏃舵暟缁勮秺鐣�"); + } + } + + /** + * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘�� + * 2浣嶅瓧鑺傛暟缁勮浆鎹负鐭暣鍨� + * @param bs 瀛楄妭鏁扮粍 + * @param from 瀛楄妭鏁扮粍璧峰浣嶇疆 + * @return 缁撴灉 + */ + public static int bytes2Short_BE(byte[] bs, int from) throws Exception { + boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); + if (!b) { + int s = 0; + int s0 = Integer.valueOf(bs[from + 0] & 0xff).shortValue();// 鏈�浣庝綅 + int s1 = Integer.valueOf(bs[from + 1] & 0xff).shortValue(); + + // 鏈�浣庝綅S1涓嶅彉 + s0 <<= 8; + s = s0 | s1 ; + if(s < 0){ + s = (Short.MAX_VALUE * 2 + 1) + s + 1; + } + return s; + } else { + throw new Exception("bytes2Short鏃舵暟缁勮秺鐣�"); + } + } + + /** + * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘�� + * 涓庢柟娉昩ytes2Short绠楁硶涓�鏍凤紝鍙槸鎶婇『搴忓弽杩囨潵 + * @param bs 瀛楄妭鏁扮粍 + * @param from 瀛楄妭鏁扮粍璧峰浣嶇疆 + * @return 缁撴灉 + */ + public static int bytes2Short_LE(byte[] bs, int from) throws Exception { + boolean b = isOutOfArrLength(bs.length, (from - 1) + 2); + if (!b) { + int s = 0; + int s0 = Integer.valueOf(bs[from + 0] & 0xff).shortValue();// 灏忎笅鏍囧瓧鑺傝浆鏁版嵁浣庝綅 + int s1 = Integer.valueOf(bs[from + 1] & 0xff).shortValue();// 澶т笅鏍囧瓧鑺傝浆鏁版嵁楂樹綅 + + // 鏈�浣庝綅S0涓嶅彉 + s1 <<= 8; + s = s1 | s0 ; + if(s < 0){ + s = (Short.MAX_VALUE * 2 + 1) + s + 1; + } + return s; + } else { + throw new Exception("bytes2Short鏃舵暟缁勮秺鐣�"); + } + } + + + /** + * 1浣嶅瓧鑺傛暟缁勮浆鎹负鐭暣鍨� + * @param bs 瀛楄妭鏁扮粍 + * @param index 瀛楄妭鏁扮粍璧峰浣嶇疆 + * @return 缁撴灉 + */ + public static short byte2Byte(byte[] bs, int index) throws Exception { + if (bs.length - 1 < index) { + throw new Exception("byte2Short(byte[] bs, int index)鏃舵暟缁勮秺鐣�"); + } else { + byte bv = (byte)(bs[index] & 0xff) ; + short s = 0 ; + if(bv < 0){ + s = (short)(Byte.MAX_VALUE * 2 + 1 + bv + 1) ; + }else{ + s = bv ; + } + return s ; + } + } + /** + * 鏃犵鍙穝hort绫诲瀷杞崲鎴�1浣峛yte + * java娌℃湁鏃犵鍙风煭鏁村瀷鏁版嵁锛屽彧鏈夋湁绗﹀彿鐭暣鏁帮紝鍙栧�艰寖鍥存槸-128~127 + * 鑻ユā鎷熸棤绗﹀彿鐭暣鍨嬫暟鎹紝鍙栧�艰寖鍥存槸0鍒�255 锛屽凡缁忚秴鍑轰簡java鐨勬湁绗﹀彿鏁存暟涓婇檺锛屾墍浠ュ彧鑳界敤java鐨剆hort鍨嬭〃绀烘棤绗﹀彿鏁存暟 + * @value bs byte[] 瀛楄妭鏁扮粍 + * @value value short short绫诲瀷鐨勫弬鏁� + * @value index 璧峰浣嶇疆 + */ + public static void byte2Byte(byte[] bs, short value, int index)throws Exception { + int maxShortUnsigned = Integer.valueOf(Byte.MAX_VALUE) * 2 + 1; + int minShortUnsigned = Integer.valueOf(Byte.MIN_VALUE) * 2 ; + if(value < minShortUnsigned || value > maxShortUnsigned ){ + throw new Exception("鏁版嵁" + value + "瓒呭嚭浜嗘棤绗﹀彿byte鍨嬬殑鍙栧�艰寖鍥�(" + minShortUnsigned + "~" + maxShortUnsigned + ")") ; + } + if (bs.length - 1 < index) { + throw new Exception("byte2Byte(byte[] bs, short value, int index)鏃舵暟缁勮秺鐣�"); + } else { + bs[index] = Integer.valueOf(value & 0xff).byteValue() ; + } + } + + /** + * 鍒ゆ柇鎵�鏈夊瓧鑺傛槸鍚︿负0xFF + * @param bs 瀛楄妭鏁扮粍 + * @param index 璧峰浣嶇疆 + * @param len 闀垮害 + * @return 缁撴灉 + * @throws Exception + */ + public static boolean bytesIsAll0xFF(byte[] bs, int index, int len)throws Exception { + int count = 0 ; + for(int i = index; i < index + len; i++){ + if(bs[i] == (byte)0xFF){ + count++ ; + } + } + return count==len?true:false ; + } + + /** + * 鍒ゆ柇鏁扮粍涓嬫爣鏄惁瓒婄晫 + * + * @value bsLength 鏁扮粍鎬婚暱搴� + * @value toSite 鏁扮粍鍋忕Щ閲� + * @return 缁撴灉 + */ + private static boolean isOutOfArrLength(int bsLength, int toSite) { + if (bsLength > toSite) { + return false; + } else { + return true; + } + } +// public static void main(String[] args) throws Exception{ +// 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{ + byte[] bs = new byte[]{0x02, 0x0C} ; + int s = ByteUtilUnsigned.bytes2Short_BE(bs, 0) ; + System.out.println(s); +//// 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