|  |  |  | 
|---|
|  |  |  | for (int i = 0; i < 4; i++) { | 
|---|
|  |  |  | bs[from + i] = Integer.valueOf(value & 0xff).byteValue();// 将最低位保存在低字节 | 
|---|
|  |  |  | value = value >> 8; // 向右移8位 | 
|---|
|  |  |  | if(value == 0){ | 
|---|
|  |  |  | break ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(value == 0){ | 
|---|
|  |  |  | break ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new Exception("int2Bytes时数组越界"); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | 
|---|
|  |  |  | //   /** | 
|---|
|  |  |  | //    * 将byte[]转换为16进制字符串 | 
|---|
|  |  |  | //    * | 
|---|
|  |  |  | //    * @param bytes 待转换byte[] | 
|---|
|  |  |  | //    * @return 返回 转换后的字符串 | 
|---|
|  |  |  | //    */ | 
|---|
|  |  |  | //   public static String bytesToHex(byte[] bytes) { | 
|---|
|  |  |  | //      //一个byte为8位,可用两个十六进制位标识 | 
|---|
|  |  |  | //      char[] buf = new char[bytes.length * 2]; | 
|---|
|  |  |  | //      int a = 0; | 
|---|
|  |  |  | //      int index = 0; | 
|---|
|  |  |  | //      for (byte b : bytes) { // 使用除与取余进行转换 | 
|---|
|  |  |  | //         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); | 
|---|
|  |  |  | //   } | 
|---|
|  |  |  | //   /** | 
|---|
|  |  |  | //    * 将byte[]转换为16进制字符串 | 
|---|
|  |  |  | //    * | 
|---|
|  |  |  | //    * @param bytes 待转换byte[] | 
|---|
|  |  |  | //    * @return 返回 转换后的字符串 | 
|---|
|  |  |  | //    */ | 
|---|
|  |  |  | //   public static String bytesToHex_BE(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] ; | 
|---|
|  |  |  | //      } | 
|---|
|  |  |  | //      //一个byte为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); | 
|---|
|  |  |  | //   } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    /** | 
|---|
|  |  |  | //     * 将byte[]转换为16进制字符串 | 
|---|
|  |  |  | //     * | 
|---|
|  |  |  | //     * @param bytes 待转换byte[] | 
|---|
|  |  |  | //     * @return 返回 转换后的字符串 | 
|---|
|  |  |  | //     */ | 
|---|
|  |  |  | //    public static String bytesToHex_LE(byte[] bytes, int startIndex, int endIndex) { | 
|---|
|  |  |  | //        byte[] bs = new byte[endIndex - startIndex + 1] ; | 
|---|
|  |  |  | //        byte j = 0 ; | 
|---|
|  |  |  | //        for(int i = endIndex; i >= startIndex; i--){ | 
|---|
|  |  |  | //            bs[j++] = bytes[i] ; | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        //一个byte为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[] | 
|---|
|  |  |  | //    * | 
|---|
|  |  |  | //    * @param str 待转换字符串 | 
|---|
|  |  |  | //    * @return 返回 转换后的byte[] | 
|---|
|  |  |  | //    */ | 
|---|
|  |  |  | //   public static byte[] hexToBytes(String str) { | 
|---|
|  |  |  | //      if (str == null || "".equals(str.trim())) { | 
|---|
|  |  |  | //         return new byte[0]; | 
|---|
|  |  |  | //      } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //      byte[] bytes = new byte[str.length() / 2]; | 
|---|
|  |  |  | //      for (int i = 0; i < str.length() / 2; i++) { | 
|---|
|  |  |  | //         String subStr = str.substring(i * 2, i * 2 + 2); | 
|---|
|  |  |  | //         bytes[i] = (byte) Integer.parseInt(subStr, 16); | 
|---|
|  |  |  | //      } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //      return bytes; | 
|---|
|  |  |  | //   } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Convert char to byte | 
|---|
|  |  |  | * @param c char | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 小端模式《数据低位在数组低字节》 | 
|---|
|  |  |  | * 整形转成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] ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 大端模式《数据低位在数组高字节》 | 
|---|
|  |  |  | * 长整形转成BCD编码 | 
|---|
|  |  |  | * @param l | 
|---|