From b3b17b231e2f2840332ce6eb96f791865fdec6d5 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 14 四月 2025 16:38:16 +0800
Subject: [PATCH] 代码优化
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java | 1554 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 1,554 insertions(+), 0 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
new file mode 100644
index 0000000..5f05861
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ByteUtil.java
@@ -0,0 +1,1554 @@
+package com.dy.common.util;
+
+import java.util.Locale;
+
+
+public class ByteUtil {
+
+ /**
+ * 灏嗗瓧鑺傛暟缁勫垎鍓�
+ * @param bytes 琚垎鍓叉暟缁�
+ * @param fromIndex 鍒嗗壊璧峰浣嶇疆
+ * @param len 鍒嗗壊闀垮害
+ * @return 杩斿洖 鍒嗗壊鍑烘潵鐨勬暟缁�
+ */
+ public static byte[] bytesSplit(byte[] bytes, int fromIndex, int len){
+ if(bytes == null){
+ return null ;
+ }else{
+ byte[] bs = new byte[len] ;
+ if(fromIndex + len > bytes.length){
+ System.arraycopy(bytes, fromIndex, bs, 0, bytes.length - fromIndex) ;
+ }else{
+ System.arraycopy(bytes, fromIndex, bs, 0, len) ;
+ }
+ return bs ;
+ }
+ }
+ /**
+ * 灏嗗瓧鑺傛暟缁勫垎鍓�
+ * @param bytes 琚垎鍓叉暟缁�
+ * @param fromIndex 鍒嗗壊璧峰浣嶇疆
+ * @param len 鍒嗗壊闀垮害
+ * @return 杩斿洖 鍒嗗壊鍑烘潵鐨勬暟缁�
+ */
+ public static Object[] bytesSplit_(byte[] bytes, int fromIndex, int len){
+ if(bytes == null){
+ return null ;
+ }else{
+ int realLen = len ;
+ byte[] bs = new byte[len] ;
+ if(fromIndex + len > bytes.length){
+ System.arraycopy(bytes, fromIndex, bs, 0, bytes.length - fromIndex) ;
+ realLen = bytes.length - fromIndex ;
+ }else{
+ System.arraycopy(bytes, fromIndex, bs, 0, len) ;
+ }
+ return new Object[]{bs, realLen} ;
+ }
+ }
+
+ /**
+ * 灏嗗瓧鑺傛暟缁勫悎骞跺埌瀛楄妭鏁扮粍涓�
+ * @param bGroup1 琚悎骞舵暟缁�
+ * @param bGroup2 鍚堝苟鏁扮粍
+ * @return 杩斿洖 鍚堝苟鍚庢暟缁�
+ */
+ public static byte[] bytesMerge(byte[] bGroup1, byte[] bGroup2){
+ if(bGroup1 == null && bGroup2 == null){
+ return null ;
+ }else if(bGroup1 == null && bGroup2 != null){
+ return bGroup2 ;
+ }else if(bGroup1 != null && bGroup2 == null){
+ return bGroup1 ;
+ }else{
+ //dest != null && append != null
+ byte[] bs = new byte[bGroup1.length + bGroup2.length] ;
+ System.arraycopy(bGroup1, 0, bs, 0, bGroup1.length) ;
+ System.arraycopy(bGroup2, 0, bs, bGroup1.length, bGroup2.length) ;
+ return bs ;
+ }
+ }
+
+ /**
+ * 鍒ゆ柇鎵�鏈夊瓧鑺傛槸鍚︿负0xFF
+ * @param bs 瀛楄妭鏁扮粍
+ * @param index 涓嬫爣浣�
+ * @param len 闀垮害
+ * @return 杩斿洖 杩斿洖
+ */
+ 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){
+ count++ ;
+ }
+ }
+ return count == len;
+ }
+
+ /**
+ * 浜岃繘鍒惰浆鍗佽繘鍒舵暟
+ * @param str 浜岃繘鍒跺瓧绗︿覆
+ * @return 杩斿洖
+ */
+ public static int binary2Int(String str) {
+ return Integer.parseInt(str, 2);
+ }
+
+
+ /**
+ * 瀛楄妭杞瓨浜岃繘鍒�
+ *
+ * @param b byte
+ * @return 杩斿洖 String
+ */
+ public static String byte2Binary(byte b) {
+ return Integer.toBinaryString(b & 0xFF) ;
+ }
+ /**
+ * 瀛楄妭杞瓨8浣嶄簩杩涘埗
+ *
+ * @param b byte
+ * @return 杩斿洖 String
+ */
+ public static String byte2bit8Binary(byte b) {
+ String s = byte2Binary(b);
+ int len = s.length();
+ for (int i = 0; i < 8 - len; i++) {
+ s = "0" + s;
+ }
+ return s;
+ }
+
+ /**
+ * 瀛楄妭鏁� 鍙栧嚭8涓猙it
+ * @param b 瀛楄妭鏁�
+ * @return 杩斿洖 String
+ */
+ public static byte[] getBit(byte b) {
+ byte[] bs = new byte[8] ;
+ bs[0] = (byte)(b & 1) ;
+ bs[1] = (byte)((b & 2) >> 1) ;
+ bs[2] = (byte)((b & 4) >> 2) ;
+ bs[3] = (byte)((b & 8) >> 3) ;
+ bs[4] = (byte)((b & 16) >> 4) ;
+ bs[5] = (byte)((b & 32) >> 5) ;
+ bs[6] = (byte)((b & 64) >> 6) ;
+ bs[7] = (byte)((b & 128) >> 7) ;
+ return bs;
+ }
+
+ /**
+ * 瀛楄妭鏁� 鍙栧嚭bit
+ * @param b 瀛楄妭鏁�
+ * @param index 涓嬫爣浣�
+ * @throws Exception 寮傚父
+ * @return 杩斿洖 String
+ */
+ public static byte getBit(byte b, byte index) throws Exception {
+ if(index == 0){
+ return (byte)(b & 1) ;
+ }else if(index == 1){
+ return (byte)((b & 2) >> 1) ;
+ }else if(index == 2){
+ return (byte)((b & 4) >> 2) ;
+ }else if(index == 3){
+ return (byte)((b & 8) >> 3) ;
+ }else if(index == 4){
+ return (byte)((b & 16) >> 4) ;
+ }else if(index == 5){
+ return (byte)((b & 32) >> 5) ;
+ }else if(index == 6){
+ return (byte)((b & 64) >> 6) ;
+ }else if(index == 7){
+ return (byte)((b & 128) >> 7) ;
+ }
+ return 0;
+ }
+
+
+ /**
+ * 涓�涓瓧鑺傝浆姝f暣鏁�
+ *
+ * @param b
+ * byte
+ * @throws Exception 寮傚父
+ * @return 杩斿洖 String
+ */
+ public static Short byte2PlusInt(byte b) throws Exception {
+ short v = b ;
+ if(v < 0){
+ v = (short)(255 + v + 1) ;
+ }
+ return v ;
+ }
+
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * double杞崲byte
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @param value double double绫诲瀷鐨勫弬鏁�
+ * @param from 寮�濮嬩綅 int
+ */
+ public static void double2Bytes_BE(byte[] bs, double value, int from)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
+ if (b) {
+ long2Bytes_BE(bs, Double.doubleToLongBits(value), from);
+ } else {
+ throw new Exception("double2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * double杞崲byte锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @param value double double绫诲瀷鐨勫弬鏁�
+ * @param from 寮�濮嬩綅 int
+ */
+ public static void double2Bytes_LE(byte[] bs, double value, int from)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
+ if (b) {
+ long2Bytes_LE(bs, Double.doubleToLongBits(value), from);
+ } else {
+ throw new Exception("double2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * byte杞崲double
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @param from 寮�濮嬩綅 int
+ */
+ public static double bytes2Double_BE(byte[] bs, int from) throws Exception {
+ long l = bytes2Long_BE(bs, from);
+ return Double.longBitsToDouble(l);
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * byte杞崲double锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @param from 寮�濮嬩綅 int
+ */
+ public static double bytes2Double_LE(byte[] bs, int from) throws Exception {
+ long l = bytes2Long_LE(bs, from);
+ return Double.longBitsToDouble(l);
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * float杞崲byte
+ * @value bs byte[]
+ * @value value float float绫诲瀷鐨勫弬鏁�
+ * @value from int
+ */
+ public static void float2Bytes_BE(byte[] bs, float value, int from)
+ throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
+ if (b) {
+ Integer it = Float.floatToIntBits(value);
+ int2Bytes_BE(bs, it, from);
+ } else {
+ throw new Exception("float2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * float杞崲byte锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @value bs byte[]
+ * @value value float float绫诲瀷鐨勫弬鏁�
+ * @value from int
+ */
+ public static void float2Bytes_LE(byte[] bs, float value, int from) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
+ if (b) {
+ Integer it = Float.floatToIntBits(value);
+ int2Bytes_LE(bs, it, from);
+ } else {
+ throw new Exception("float2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * byte杞崲float
+ * @value bs byte[]
+ * @value from int
+ */
+ public static float bytes2Float_BE(byte[] bs, int from) throws Exception {
+ int i = bytes2Int_BE(bs, from);
+ return Float.intBitsToFloat(i);
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * byte杞崲float锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @value bs byte[]
+ * @value from int
+ */
+ public static float bytes2Float_LE(byte[] bs, int from) throws Exception {
+ int i = bytes2Int_LE(bs, from);
+ return Float.intBitsToFloat(i);
+ }
+
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 杞崲long鍨嬩负byte鏁扮粍
+ * @value bs byte[]
+ * @value value long
+ * @value from int
+ */
+ public static void long2Bytes_BE(byte[] bs, long value, int from)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
+ if (b) {
+ for (int i = 7; i >= 0; i--) {
+ bs[from + i] = Long.valueOf(value & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄦ渶浣庝綅
+ value = value >> 8; // 鍚戝彸绉�8浣�
+ if(value == 0){
+ break ;
+ }
+ }
+ } else {
+ throw new Exception("long2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 杞崲long鍨嬩负byte鏁扮粍
+ * @value bs byte[]
+ * @value value long
+ * @value from int
+ */
+ public static void long2Bytes_LE(byte[] bs, long value, int from)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
+ if (b) {
+ for (int i = 0; i < 8; i++) {
+ bs[from + i] = Long.valueOf(value & 0xff).byteValue();
+ value = value >> 8;
+ if(value == 0){
+ break ;
+ }
+ }
+ } else {
+ throw new Exception("long2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 8浣嶅瓧鑺傛暟缁勮浆鎹负闀挎暣鍨�
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @return 杩斿洖
+ */
+ public static long bytes2Long_BE(byte[] bs) {
+ int len = bs.length ;
+ if (len > 0) {
+ long l = 0;
+ long[] ls = new long[len] ;
+ for(int i = 0 ; i < len; i++){
+ ls[i] = bs[i] & 0xFF ;
+ }
+ for(int i = len-1 ; i >= 0; i--){
+ ls[(len - 1) - i] <<= 8 * i ;
+ }
+ for(int i = 0 ; i < len; i++){
+ l = l | ls[i] ;
+ }
+ return l;
+ }
+ return 0L ;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 8浣嶅瓧鑺傛暟缁勮浆鎹负闀挎暣鍨�
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @param from 寮�濮嬩綅 int
+ * @return 杩斿洖
+ */
+ public static long bytes2Long_BE(byte[] bs, int from) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
+ 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 ;
+ 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;
+ s5 <<= 16;
+ s4 <<= 24;
+ s3 <<= 8 * 4;
+ s2 <<= 8 * 5;
+ s1 <<= 8 * 6;
+ s0 <<= 8 * 7;
+ s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
+ return s;
+ } else {
+ throw new Exception("byte2Long鏃舵暟缁勮秺鐣�");
+ }
+ }
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 8浣嶅瓧鑺傛暟缁勮浆鎹负闀挎暣鍨�
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @param from 寮�濮嬩綅 int
+ * @param end int
+ * @return 杩斿洖
+ */
+ public static long bytes2Long_BE(byte[] bs, int from, int end) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, end);
+ if (b) {
+ short len = (short)(end - from + 1) ;
+ long[] ls = new long[len] ;
+ for(short i = 0 ; i < len; i++){
+ ls[i] = bs[from + i] & 0xFF ;
+ }
+ for(short i = (short)(len-1) ; i >= 0; i--){
+ ls[i] <<= 8 * (len - (i + 1)) ;
+ }
+ long s = 0;
+ for(short i = 0 ; i < len; i++){
+ s = s | ls[i] ;
+ }
+ return s;
+ } else {
+ throw new Exception("byte2Long鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 8浣嶅瓧鑺傛暟缁勮浆鎹负闀挎暣鍨�
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @return 杩斿洖
+ */
+ public static long bytes2Long_LE(byte[] bs) {
+ int len = bs.length ;
+ if (len > 0) {
+ long l = 0;
+ long[] ls = new long[len] ;
+ for(int i = 0 ; i < len; i++){
+ ls[i] = bs[i] & 0xFF ;
+ }
+ for(int i = 0 ; i < len; i++){
+ ls[(len - 1) - i] <<= 8 * i ;
+ }
+ for(int i = 0 ; i < len; i++){
+ l = l | ls[i] ;
+ }
+ return l;
+ }
+ return 0L ;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 8浣嶅瓧鑺傛暟缁勮浆鎹负闀挎暣鍨�
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @param from 寮�濮嬩綅 int
+ * @return 杩斿洖
+ */
+ public static long bytes2Long_LE(byte[] bs, int from) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 8);
+ if (b) {
+ long s = 0;
+ 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;
+ s2 <<= 16;
+ s3 <<= 24;
+ s4 <<= 8 * 4;
+ s5 <<= 8 * 5;
+ s6 <<= 8 * 6;
+ s7 <<= 8 * 7;
+ s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
+ return s;
+ } else {
+ throw new Exception("byte2Long鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 8浣嶅瓧鑺傛暟缁勮浆鎹负闀挎暣鍨�
+ * @param bs 瀛楄妭鏁扮粍 byte[]
+ * @param from 寮�濮嬩綅 int
+ * @param end int
+ * @return 杩斿洖
+ */
+ public static long bytes2Long_LE(byte[] bs, int from, int end) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, end);
+ if (b) {
+ short len = (short)(end - from + 1) ;
+ long[] ls = new long[len] ;
+ for(short i = 0 ; i < len; i++){
+ ls[i] = bs[from + i] & 0xFF ;
+ }
+ for(short i = 0 ; i < len; i++){
+ ls[i] <<= 8 * i ;
+ }
+ long s = 0;
+ for(short i = 0 ; i < len; i++){
+ s = s | ls[i] ;
+ }
+ return s;
+ } else {
+ throw new Exception("byte2Long鏃舵暟缁勮秺鐣�");
+ }
+ }
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * int绫诲瀷杞崲鎴�4浣峛yte鏁扮粍
+ * @value bs byte[]
+ * @value value int int绫诲瀷鐨勫弬鏁�
+ * @value from int
+ */
+ public static void int2Bytes_BE(byte[] bs, int value, int from)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
+ if (b) {
+ for (int i = 3; i >= 0; i--) {
+ bs[from + i] = Integer.valueOf(value & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄩ珮瀛楄妭
+ value = value >> 8; // 鍚戝彸绉�8浣�
+ if(value == 0){
+ break ;
+ }
+ }
+ } else {
+ throw new Exception("int2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * int绫诲瀷杞崲鎴�4浣峛yte鏁扮粍锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @value bs byte[]
+ * @value value int int绫诲瀷鐨勫弬鏁�
+ * @value from int
+ */
+ public static void int2Bytes_LE(byte[] bs, int value, int from)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
+ if (b) {
+ for (int i = 0; i < 4; i++) {
+ bs[from + i] = Integer.valueOf(value & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄤ綆瀛楄妭
+ value = value >> 8; // 鍚戝彸绉�8浣�
+ if(value == 0){
+ break ;
+ }
+ }
+ } else {
+ throw new Exception("int2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 4浣嶅瓧鑺傛暟缁勮浆鎹负鏁村瀷
+ * @param bs 瀛楄妭鏁扮粍
+ * @param from 寮�濮嬩綅
+ * @return 杩斿洖
+ */
+ public static int bytes2Int_BE(byte[] bs, int from) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
+ if (b) {
+ int s = 0;
+ 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;
+ s1 <<= 16;
+ s0 <<= 24;
+ s = s0 | s1 | s2 | s3;
+ return s;
+ } else {
+ throw new Exception("byte2Int鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 4浣嶅瓧鑺傛暟缁勮浆鎹负鏁村瀷锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param bs 瀛楄妭鏁扮粍 瀛楄妭鏁扮粍
+ * @return 杩斿洖
+ */
+ public static int bytes2Int_LE(byte[] bs, int from) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 4);
+ if (b) {
+ int s = 0;
+ 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;
+ s2 <<= 16;
+ s3 <<= 24;
+ s = s0 | s1 | s2 | s3;
+ return s;
+ } else {
+ throw new Exception("byte2Int鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * short绫诲瀷杞崲鎴恇yte鏁扮粍
+ * @value bs byte[]
+ * @value value short
+ * @value from int
+ */
+ public static void short2Bytes_BE(byte[] bs, short value, int from)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 2);
+ if (b) {
+ for (int i = 1; i >= 0 ; i--) {
+ bs[from + i] = Integer.valueOf(value & 0xff).byteValue();// 灏嗕綆浣嶄繚瀛樺湪楂樺瓧鑺�
+ value = (short) (value >> 8); // 鍚戝彸绉�8浣�
+ }
+ } else {
+ throw new Exception("short2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * short绫诲瀷杞崲鎴恇yte鏁扮粍锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @value bs byte[]
+ * @value value short
+ * @value from int
+ */
+ public static void short2Bytes_LE(byte[] bs, short value, int from)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 2);
+ if (b) {
+ for (int i = 0; i < 2; i++) {
+ bs[from + i] = Integer.valueOf(value & 0xff).byteValue();// 灏嗘渶浣庝綅淇濆瓨鍦ㄤ綆瀛楄妭
+ value = (short) (value >> 8); // 鍚戝彸绉�8浣�
+ }
+ } else {
+ throw new Exception("short2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * short绫诲瀷杞崲鎴恇yte鏁扮粍
+ * @value value short
+ * @value from int
+ */
+ public static byte[] short2Bytes_BE(short value)throws Exception {
+ byte[] bs = new byte[2] ;
+ for (int i = 1; i >= 0 ; i--) {
+ bs[i] = (byte)(value & 0xff) ;// 灏嗕綆浣嶄繚瀛樺湪楂樺瓧鑺�
+ value = (short) (value >> 8); // 鍚戝彸绉�8浣�
+ }
+ return bs ;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * short绫诲瀷杞崲鎴恇yte鏁扮粍锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @value value short
+ * @value from int
+ */
+ public static byte[] short2Bytes_LE(short value)throws Exception {
+ byte[] bs = new byte[2] ;
+ for (int i = 0; i < 2; i++) {
+ bs[i] = (byte)(value & 0xff) ;// 灏嗘渶浣庝綅淇濆瓨鍦ㄤ綆瀛楄妭
+ value = (short) (value >> 8); // 鍚戝彸绉�8浣�
+ }
+ return bs ;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 2浣嶅瓧鑺傛暟缁勮浆鎹负鐭暣鍨�
+ * @param bs 瀛楄妭鏁扮粍 瀛楄妭鏁扮粍
+ * @return 杩斿洖
+ */
+ public static short bytes2Short_BE(byte[] bs, int from) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 2);
+ if (b) {
+ int s = 0;
+ int s0 = bs[from + 0] & 0xFF ;
+ int s1 = bs[from + 1] & 0xFF ;
+
+ // s1涓嶅彉
+ s0 <<= 8;
+ s = s0 | s1;
+ return (short)s ;
+ } else {
+ throw new Exception("byte2Short鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 2浣嶅瓧鑺傛暟缁勮浆鎹负鐭暣鍨嬶紝瀛楄妭椤哄簭鏄�掔殑
+ * @param bs 瀛楄妭鏁扮粍 瀛楄妭鏁扮粍
+ * @return 杩斿洖
+ */
+ public static short bytes2Short_LE(byte[] bs, int from) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, (from - 1) + 2);
+ if (b) {
+ int s = 0;
+ int s0 = bs[from + 0] & 0xFF ;
+ int s1 = bs[from + 1] & 0xFF ;
+
+ // s0涓嶅彉
+ s1 <<= 8;
+ s = s0 | s1;
+ return (short) s;
+
+ } else {
+ throw new Exception("byte2Short鏃舵暟缁勮秺鐣�");
+ }
+ }
+ /**
+ * 瀛楃鍒颁竴瀛楄妭杞崲
+ *
+ * @value bs byte[]
+ * @value ch char char绫诲瀷鐨勫弬鏁�
+ * @value index int
+ * @return 杩斿洖
+ */
+ public static void char2Bytes(byte[] bs, char ch, int index)throws Exception {
+ boolean b = isOutOfArrLength(bs.length, index);
+ if (b) {
+ bs[index] = (byte) ch;
+ } else {
+ throw new Exception("char2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 涓�瀛楄妭杞崲涓哄瓧绗�
+ * @param bs 瀛楄妭鏁扮粍 瀛楄妭鏁扮粍
+ * @value index int
+ * @return 杩斿洖
+ */
+ public static char bytes2Char(byte[] bs, int index) throws Exception {
+ boolean b = isOutOfArrLength(bs.length, index);
+ if (b) {
+ return (char) bs[index];
+ } else {
+ throw new Exception("byte2Char鏃舵暟缁勮秺鐣�");
+ }
+ }
+
+ /**
+ * 瀛楃涓插瀷鏁板瓧杞垚byte
+ *
+ * @param s
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static byte string2byte(String s) throws Exception {
+ int n ;
+ try {
+ n = Integer.parseInt(s);
+ } catch (Exception e) {
+ throw new Exception("瀛楃涓插瀷鏁板瓧瀛楄妭鏃跺嚭閿欙紝涓嶆槸鍚堟硶鏁板瓧:" + s, null);
+ }
+ return (byte) n;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 瀛楃涓茶浆鎹㈡垚byte鏁扮粍
+ * @value bs byte[]
+ * @value str String
+ * @value from int
+ * @return 杩斿洖
+ * @throws java.io.UnsupportedEncodingException
+ */
+ public static int string2Bytes_BE(byte[] bs, String str, int from, int end)throws Exception {
+ byte[] bb = str.getBytes();
+ boolean b = isOutOfArrLength(bs.length, (from -1 + bb.length));
+ if (b) {
+ if(end - from + 1 < bb.length){
+ throw new Exception("string2Bytes鏃讹紝瀛楃涓茶浆鎴愮殑瀛楄妭鏁扮粍瓒呰繃鐨勫崗璁畾涔夌殑闀垮害 ");
+ }else{
+ for (int i = 0; i < bb.length; i++) {
+ bs[from + i] = bb[i];
+ }
+ }
+ } else {
+ throw new Exception("string2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ return bb.length ;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 瀛楃涓茶浆鎹㈡垚byte鏁扮粍
+ * @value bs byte[]
+ * @value str String
+ * @value from int
+ * @return 杩斿洖
+ * @throws java.io.UnsupportedEncodingException
+ */
+ public static int string2Bytes_LE(byte[] bs, String str, int from, int end)throws Exception {
+ byte[] bb = str.getBytes();
+ boolean b = isOutOfArrLength(bs.length, (from -1 + bb.length));
+ if (b) {
+ if(end - from + 1 < bb.length){
+ throw new Exception("string2Bytes鏃讹紝瀛楃涓茶浆鎴愮殑瀛楄妭鏁扮粍瓒呰繃鐨勫崗璁畾涔夌殑闀垮害 ");
+ }else{
+ for (int i = bb.length - 1; i >= 0; i--) {
+ bs[from + i] = bb[i];
+ }
+ }
+ } else {
+ throw new Exception("string2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ return bb.length ;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 瀛楃涓茶浆鎹㈡垚byte鏁扮粍
+ * @value bs byte[]
+ * @value str String
+ * @value from int
+ * @return 杩斿洖
+ * @throws java.io.UnsupportedEncodingException
+ */
+ public static int string2Bytes_BE(byte[] bs, String str, int from)throws Exception {
+ byte[] bb = str.getBytes();
+ boolean b = isOutOfArrLength(bs.length, (from -1 + bb.length));
+ if (b) {
+ for (int i = 0; i < bb.length; i++) {
+ bs[from + i] = bb[i];
+ }
+ } else {
+ throw new Exception("string2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ return bb.length ;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 瀛楃涓茶浆鎹㈡垚byte鏁扮粍
+ * @value bs byte[]
+ * @value str String
+ * @value from int
+ * @return 杩斿洖
+ * @throws java.io.UnsupportedEncodingException
+ */
+ public static int string2Bytes_LE(byte[] bs, String str, int from)throws Exception {
+ byte[] bb = str.getBytes();
+ boolean b = isOutOfArrLength(bs.length, (from -1 + bb.length));
+ if (b) {
+ for (int i = bb.length-1; i >= 0; i--) {
+ bs[from + i] = bb[i];
+ }
+ } else {
+ throw new Exception("string2Bytes鏃舵暟缁勮秺鐣�");
+ }
+ return bb.length ;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * byte鏁扮粍杞崲鎴愬瓧绗︿覆
+ * @value bs byte[]
+ * @value str String
+ * @value from int
+ * @throws java.io.UnsupportedEncodingException
+ */
+ public static String bytes2String_BE(byte[] bs, int from, int end)throws Exception {
+ byte[] bb = new byte[end - from + 1];
+ int count = 0 ;
+ for (int i = from; i <= end; i++) {
+ bb[count++] = bs[i];
+ }
+ return new String(bb);
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * byte鏁扮粍杞崲鎴愬瓧绗︿覆
+ * @value bs byte[]
+ * @value str String
+ * @value from int
+ * @throws java.io.UnsupportedEncodingException
+ */
+ public static String bytes2String_LE(byte[] bs, int from, int end)throws Exception {
+ byte[] bb = new byte[end - from + 1];
+ int count = 0 ;
+ for (int i = end; i >= from; i--) {
+ bb[count++] = bs[i];
+ }
+ return new String(bb);
+ }
+
+ /**
+ * 鍒ゆ柇鏁扮粍涓嬫爣鏄惁瓒婄晫
+ *
+ * @value bsLength 鏁扮粍鎬婚暱搴�
+ * @value toSite 鏁扮粍鍋忕Щ閲�
+ * @return 杩斿洖
+ */
+ private static boolean isOutOfArrLength(int bsLength, int toSite) {
+ if (bsLength > toSite) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+
+ /**
+ * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓�
+ *
+ * @param src byte[]
+ * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅�
+ * @return 杩斿洖 String
+ */
+ public static String bytes2Hex(byte[] src, boolean hasBlank){
+ return bytes2Hex_BE(src, hasBlank) ;
+ }
+
+ /**
+ * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓�
+ *
+ * @param src byte[]
+ * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅�
+ * @return 杩斿洖 String
+ */
+ public static String bytes2Hex_BE(byte[] src, boolean hasBlank){
+ StringBuilder stringBuilder = new StringBuilder("");
+ if (src == null || src.length <= 0) {
+ return null;
+ }
+ for (int i = 0; i < src.length; i++) {
+ int v = src[i] & 0xFF;
+ String str = Integer.toHexString(v);
+ if (str.length() < 2) {
+ str = "0" + str;
+ }
+ if (hasBlank) {
+ if (i == 0) {
+ stringBuilder.append(str);
+ } else {
+ stringBuilder.append(" " + str);
+ }
+ } else {
+ stringBuilder.append(str);
+ }
+ }
+ return stringBuilder.toString().toUpperCase(Locale.US);
+ }
+ /**
+ * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓�
+ *
+ * @param src byte[]
+ * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅�
+ * @return 杩斿洖 String
+ */
+ public static String bytes2Hex_LE(byte[] src, boolean hasBlank){
+ StringBuilder stringBuilder = new StringBuilder("");
+ if (src == null || src.length <= 0) {
+ return null;
+ }
+ for (int i = src.length - 1 ; i >= 0; i--) {
+ int v = src[i] & 0xFF;
+ String str = Integer.toHexString(v);
+ if (str.length() < 2) {
+ str = "0" + str;
+ }
+ if (hasBlank) {
+ if (i == 0) {
+ stringBuilder.append(str);
+ } else {
+ stringBuilder.append(" " + str);
+ }
+ } else {
+ stringBuilder.append(str);
+ }
+ }
+ return stringBuilder.toString().toUpperCase(Locale.US);
+ }
+ /**
+ * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓�
+ *
+ * @param bs byte[]
+ * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅�
+ * @param from 寮�濮嬩綅
+ * @param len 闀垮害
+ * @return 杩斿洖 String
+ */
+ public static String bytes2Hex(byte[] bs, boolean hasBlank, int from, int len){
+ if (bs == null || bs.length <= 0 || bs.length < from + len) {
+ return null;
+ }
+ byte[] bb = new byte[len];
+ for (int i = 0 ; i < len; i++) {
+ bb[i] = bs[from + i];
+ }
+ return bytes2Hex(bb, hasBlank) ;
+ }
+ /**
+ * 瀛楄妭鏁扮粍杞崲鎴愬崄鍏繘鍒剁殑瀛楃涓�
+ *
+ * @param bs byte[]
+ * @param hasBlank 16杩涘埗鏄惁鐢ㄧ┖鏍煎垎闅�
+ * @param from 寮�濮嬩綅
+ * @param len 闀垮害
+ * @return 杩斿洖 String
+ */
+ public static String bytes2Hex_LE(byte[] bs, boolean hasBlank, int from, int len){
+ if (bs == null || bs.length <= 0 || bs.length < from + len) {
+ return null;
+ }
+ byte[] bb = new byte[len];
+ for (int i = 0 ; i < len; i++) {
+ bb[i] = bs[from + i];
+ }
+ return bytes2Hex_LE(bb, hasBlank) ;
+ }
+ /**
+ * 鍗佸叚杩涘埗杞瓧鑺傛暟缁�
+ * @param hex the hex string
+ * @return 杩斿洖 byte[]
+ */
+ public static byte[] hex2Bytes(String hex) {
+ if (hex == null || hex.equals("")) {
+ return null;
+ }
+ hex = hex.toUpperCase(Locale.ENGLISH);
+ int length = hex.length() / 2;
+ char[] hexChars = hex.toCharArray();
+ byte[] d = new byte[length];
+ for (int i = 0; i < length; i++) {
+ int pos = i * 2;
+ d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
+ }
+ return d;
+ }
+ /**
+ * 鍗佸叚杩涘埗杞瓧鑺傛暟缁�
+ * @param hex the hex string
+ * @return 杩斿洖 byte[]
+ */
+ public static int hex2Bytes(String hex, byte[] bs, int fromIndex) {
+ if (hex == null || hex.equals("")) {
+ return fromIndex;
+ }
+ hex = hex.toUpperCase(Locale.ENGLISH);
+ int length = hex.length() / 2;
+ char[] hexChars = hex.toCharArray();
+ byte[] d = new byte[length];
+ for (int i = 0; i < length; i++) {
+ int pos = i * 2;
+ d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
+ }
+ for(int i = 0 ; i < d.length; i++){
+ bs[fromIndex++] = d[i] ;
+ }
+ return fromIndex ;
+ }
+
+ /**
+ * 鍗佸叚杩涘埗杞瓧鑺傛暟缁�
+ * @param hex the hex string
+ * @return 杩斿洖 byte[]
+ */
+ public static int hex2Bytes_LE(String hex, byte[] bs, int fromIndex) {
+ if (hex == null || hex.equals("")) {
+ return fromIndex;
+ }
+ hex = hex.toUpperCase(Locale.ENGLISH);
+ int length = hex.length() / 2;
+ char[] hexChars = hex.toCharArray();
+ byte[] d = new byte[length];
+ for (int i = 0; i < length; i++) {
+ int pos = i * 2;
+ d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
+ }
+ for(int i = 0 ; i < d.length; i++){
+ bs[fromIndex++] = d[(d.length - 1) - i] ;
+ }
+ return fromIndex ;
+ }
+
+
+ /**
+ * Convert char to byte
+ * @param c char
+ * @return 杩斿洖 byte
+ */
+ private static byte charToByte(char c) {
+ return (byte) "0123456789ABCDEF".indexOf(c);
+ }
+
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 鏁村舰杞垚BCD缂栫爜
+ * @param i
+ * @return 杩斿洖
+ */
+ public static byte[] int2BCD_BE(int i)throws Exception {
+ String str = "" + i;
+ byte[] b = null;
+ if (str.length() % 2 == 0) {
+ b = new byte[str.length() / 2];
+ } else {
+ b = new byte[(str.length() / 2) + 1];
+ }
+ encodeBCD_BE(str, b, 0, b.length);
+
+ return b;
+ }
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 鏁村舰杞垚BCD缂栫爜锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param i
+ * @return 杩斿洖
+ */
+ public static byte[] int2BCD_LE(int i)throws Exception {
+ String str = "" + i;
+ byte[] b = null;
+ if (str.length() % 2 == 0) {
+ b = new byte[str.length() / 2];
+ } else {
+ b = new byte[(str.length() / 2) + 1];
+ }
+ encodeBCD_LE(str, b, 0, b.length);
+
+ return b;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 鏁村舰杞垚BCD缂栫爜锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param i
+ * @param bs
+ * @param from
+ * @return 杩斿洖
+ */
+ public static void int2BCD_LE(int i, byte[] bs, int from)throws Exception {
+ String str = "" + i;
+ byte[] b = null;
+ if (str.length() % 2 == 0) {
+ b = new byte[str.length() / 2];
+ } else {
+ b = new byte[(str.length() / 2) + 1];
+ }
+ encodeBCD_LE(str, b, 0, b.length);
+
+ int len = bs.length ;
+ int bLen = b.length ;
+ for(int j = 0; (j < len && j < bLen); j++){
+ bs[from + j] = b[j] ;
+ }
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 闀挎暣褰㈣浆鎴怋CD缂栫爜
+ * @param l
+ * @return 杩斿洖
+ */
+ public static byte[] long2BCD_BE(long l)throws Exception {
+ String str = "" + l;
+ byte[] b = null;
+ if (str.length() % 2 == 0) {
+ b = new byte[str.length() / 2];
+ } else {
+ b = new byte[(str.length() / 2) + 1];
+ }
+ encodeBCD_BE(str, b, 0, b.length);
+
+ return b;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 闀挎暣褰㈣浆鎴怋CD缂栫爜锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param l
+ * @return 杩斿洖
+ */
+ public static byte[] long2BCD_LE(long l) throws Exception {
+ String str = "" + l;
+ byte[] b = null;
+ if (str.length() % 2 == 0) {
+ b = new byte[str.length() / 2];
+ } else {
+ b = new byte[(str.length() / 2) + 1];
+ }
+ encodeBCD_LE(str, b, 0, b.length);
+
+ return b;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 瀛楃涓插瀷鏁板瓧杞垚BCD缂栫爜
+ * @param s
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static byte[] string2BCD_BE(String s) throws Exception {
+ byte[] b ;
+ if (s.length() % 2 == 0) {
+ b = new byte[s.length() / 2];
+ } else {
+ b = new byte[(s.length() / 2) + 1];
+ }
+
+ encodeBCD_BE(s, b, 0, b.length);
+
+ return b ;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 瀛楃涓茶浆鎹㈡垚byte鏁扮粍
+ * @value bs byte[]
+ * @value str String
+ * @value fromIndex int
+ * @return 杩斿洖
+ * @throws Exception 寮傚父 寮傚父
+ */
+ public static int string2BCD_BE(byte[] bs, String str, int fromIndex)throws Exception {
+ byte[] bb = string2BCD_BE(str);
+ boolean b = isOutOfArrLength(bs.length, (fromIndex -1 + bb.length));
+ if (b) {
+ for (int i = 0; i < bb.length; i++) {
+ bs[fromIndex + i] = bb[i];
+ }
+ } else {
+ throw new Exception("string2BCD鏃舵暟缁勮秺鐣�");
+ }
+ return bb.length ;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 瀛楃涓插瀷鏁板瓧杞垚BCD缂栫爜锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param s
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static byte[] string2BCD_LE(String s) throws Exception {
+ byte[] b = null;
+ if (s.length() % 2 == 0) {
+ b = new byte[s.length() / 2];
+ } else {
+ b = new byte[(s.length() / 2) + 1];
+ }
+ encodeBCD_LE(s, b, 0, b.length);
+
+ return b;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 瀛楃涓茶浆鎹㈡垚byte鏁扮粍
+ * @value bs byte[]
+ * @value str String
+ * @value fromIndex int
+ * @return 杩斿洖
+ * @throws Exception 寮傚父 寮傚父
+ */
+ public static int string2BCD_LE(byte[] bs, String str, int fromIndex)throws Exception {
+ byte[] bb = string2BCD_LE(str);
+ boolean b = isOutOfArrLength(bs.length, (fromIndex -1 + bb.length));
+ if (b) {
+ for (int i = bb.length-1; i >= 0 ; i--) {
+ bs[fromIndex + i] = bb[i];
+ }
+ } else {
+ throw new Exception("string2BCD鏃舵暟缁勮秺鐣�");
+ }
+ return bb.length ;
+ }
+
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * BCD缂栫爜杞垚鏁村瀷
+ * @param b
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static int BCD2Int_BE(byte b) throws Exception {
+ String str = "";
+ str = decodeBCD_BE(new byte[] { b }, 0, 1);
+ int n = Integer.parseInt(str);
+ return n;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * BCD缂栫爜杞垚鏁村瀷锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param b
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static int BCD2Int_LE(byte b) throws Exception {
+ String str = "";
+ str = decodeBCD_LE(new byte[] { b }, 0, 1);
+ int n = Integer.parseInt(str);
+ return n;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * BCD缂栫爜杞垚鏁村瀷
+ * @param b
+ * @param startIndex
+ * @param endIndex
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static int BCD2Int_BE(byte[] b, int startIndex, int endIndex)throws Exception {
+ String str = "";
+ str = decodeBCD_BE(b, startIndex, endIndex - startIndex + 1);
+ int n = Integer.parseInt(str);
+ return n;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * BCD缂栫爜杞垚鏁村瀷锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param b
+ * @param startIndex
+ * @param endIndex
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static int BCD2Int_LE(byte[] b, int startIndex, int endIndex)throws Exception {
+ String str = "";
+ str = decodeBCD_LE(b, startIndex, endIndex - startIndex + 1);
+ int n = Integer.parseInt(str);
+ return n;
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * BCD缂栫爜杞垚瀛楃涓插瀷
+ * @param b
+ * @param startIndex
+ * @param endIndex
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static long BCD2Long_BE(byte[] b, int startIndex, int endIndex)throws Exception {
+ String str = "";
+ str = decodeBCD_BE(b, startIndex, endIndex - startIndex + 1);
+ long n = Long.parseLong(str);
+ return n;
+ }
+
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * BCD缂栫爜杞垚瀛楃涓插瀷锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param b
+ * @param startIndex
+ * @param endIndex
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static long BCD2Long_LE(byte[] b, int startIndex, int endIndex)throws Exception {
+ String str = "";
+ str = decodeBCD_LE(b, startIndex, endIndex - startIndex + 1);
+ return Long.parseLong(str);
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * BCD缂栫爜杞垚瀛楃涓插瀷
+ * @param b
+ * @param startIndex
+ * @param endIndex
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static String BCD2String_BE(byte[] b, int startIndex, int endIndex) throws Exception {
+ return decodeBCD_BE(b, startIndex, endIndex - startIndex + 1);
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * BCD缂栫爜杞垚瀛楃涓插瀷锛屽瓧鑺傞『搴忔槸鍊掔殑
+ * @param b
+ * @param startIndex
+ * @param endIndex
+ * @return 杩斿洖
+ * @throws Exception 寮傚父
+ */
+ public static String BCD2String_LE(byte[] b, int startIndex, int endIndex) throws Exception {
+ return decodeBCD_LE(b, startIndex, endIndex - startIndex + 1);
+ }
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 缂栫爜BCD锛屼緥濡�1387缂栫爜鎴� 13 87锛岄『搴忔槸姝g殑
+ * @param value
+ * @param dest
+ * @param startIndex
+ * @param length 闀垮害
+ */
+ private static void encodeBCD_BE(String value, byte[] dest, int startIndex, int length)throws Exception {
+ if (value == null || !value.matches("\\d*")) {
+ throw new Exception("鏁板瓧杞垚BCD缂栫爜鏃跺嚭閿欙紝涓嶆槸鍚堟硶鏁板瓧:" + value, null);
+ }
+
+ int[] tmpInts = new int[2 * length];
+ int index = value.length() - 1;
+ for (int i = tmpInts.length - 1; i >= 0 && index >= 0; i--, index--) {
+ tmpInts[i] = value.charAt(index) - '0';
+ }
+ for (int i = startIndex, j = 0; i < startIndex + length; i++, j++) {
+ dest[i] = (byte) (tmpInts[2 * j] * 16 + tmpInts[2 * j + 1]);
+ }
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 缂栫爜BCD锛屼緥濡�1387缂栫爜鎴� 87 13锛岄『搴忔槸鍊掔殑
+ * @param value
+ * @param dest
+ * @param startIndex
+ * @param length 闀垮害
+ */
+ private static void encodeBCD_LE(String value, byte[] dest, int startIndex, int length)throws Exception {
+ if (value == null || !value.matches("\\d*")) {
+ throw new Exception("鏁板瓧杞垚BCD缂栫爜鏃跺嚭閿欙紝涓嶆槸鍚堟硶鏁板瓧:" + value, null);
+ }
+
+ int[] tmpInts = new int[2 * length];
+ int index = value.length() - 1;
+ for (int i = 0; i <= tmpInts.length - 1 && index >= 0; i++, index--) {
+ tmpInts[i] = value.charAt(index) - '0';
+ }
+ for (int i = startIndex, j = 0; i < startIndex + length; i++, j++) {
+ dest[i] = (byte) (tmpInts[2 * j + 1] * 16 + tmpInts[2 * j] );
+ }
+ }
+
+
+
+ /**
+ * 澶х妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍楂樺瓧鑺傘��
+ * 瑙g爜BCD锛岄『搴忔槸姝g殑
+ * @param src
+ * @param startIndex
+ * @param length 闀垮害
+ * @return 杩斿洖
+ */
+ private static String decodeBCD_BE(byte[] src, int startIndex, int length)throws Exception {
+ StringBuilder sb = new StringBuilder();
+ for (int i = startIndex; i < startIndex + length ; i++) {
+ int value = (src[i] + 256) % 256;
+ sb.append((char) (value / 16 + '0')).append((char) (value % 16 + '0'));
+ value++;
+ }
+ String result = sb.toString();
+ if (!result.matches("\\d*")) {
+ throw new Exception("瑙g爜BCD锛屼絾鏁版嵁锛�" + result + "[startIndex=" + startIndex + ",length=" + length + "]锛夐潪BCD鐮侊紒");
+ }
+ return result;
+ }
+
+ /**
+ * 灏忕妯″紡銆婃暟鎹綆浣嶅湪鏁扮粍浣庡瓧鑺傘��
+ * 缂栫爜BCD锛岄『搴忔槸鍊掔殑
+ * @param src
+ * @param startIndex
+ * @param length 闀垮害
+ * @return 杩斿洖
+ */
+ private static String decodeBCD_LE(byte[] src, int startIndex, int length)throws Exception {
+ StringBuilder sb = new StringBuilder();
+ for (int i = (startIndex + length - 1); i >= startIndex; i--) {
+ int value = (src[i] + 256) % 256;
+ sb.append((char) (value / 16 + '0')).append((char) (value % 16 + '0'));
+ }
+
+ String result = sb.toString();
+ if (!result.matches("\\d*")) {
+ throw new Exception("瑙g爜BCD锛屼絾鏁版嵁锛�" + result + "[startIndex=" + startIndex + ",length=" + length + "]锛夐潪BCD鐮侊紒");
+ }
+ return result;
+ }
+
+// public static void main(String[] args) throws Exception {
+// // 甯уご + 甯ч暱搴� + 缁堢ID + 鍔熻兘鐮� + 鏁版嵁
+// int len = 2 + 4 + 4 + 2 + (4 + 4);
+//
+// byte[] b = new byte[len];
+// ByteUtil.int2Bytes(b, 1234567890, 0);
+// int v1 = ByteUtil.bytes2Int(b, 0);
+// System.out.println(v1);
+//
+// b = new byte[len];
+// ByteUtil.short2Bytes(b, (short) -1234, 0);
+// short v2 = ByteUtil.bytes2Short(b, 0);
+// System.out.println(v2);
+//
+// b = new byte[len];
+// ByteUtil.long2Bytes(b, 4638387438405602509L, 0);
+// long v3 = ByteUtil.bytes2Long(b, 0);
+// System.out.println(v3);
+//
+// b = new byte[len];
+// ByteUtil.float2Bytes(b, (float) -123456.45, 0);
+// float v4 = ByteUtil.bytes2Float(b, 0);
+// System.out.println(v4);
+//
+// b = new byte[len];
+// ByteUtil.double2Bytes(b, -256.1234567890123D, 0);
+// double v5 = ByteUtil.bytes2Double(b, 0);
+// 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) ;
+ System.out.println(s);
+
+ byte[] bss = new byte[]{(byte)0x8F} ;
+ int v = bss[0] ;
+ System.out.println(v);
+ if(v < 0){
+ v = 255 + v + 1 ;
+ }
+ System.out.println(v);
+ }
+ */
+
+}
--
Gitblit v1.8.0