| | |
| | | * 将字节数组合并到字节数组上 |
| | | * @param bGroup1 被合并数组 |
| | | * @param bGroup2 合并数组 |
| | | * @return 返回 合并后数组 |
| | | * @return 合并后数组 |
| | | */ |
| | | public static byte[] bytesMerge(byte[] bGroup1, byte[] bGroup2){ |
| | | if(bGroup1 == null && bGroup2 == null){ |
| | |
| | | |
| | | /** |
| | | * 判断所有字节是否为0xFF |
| | | * @param bs 字节数组 |
| | | * @param index 下标位 |
| | | * @param len 长度 |
| | | * @return 返回 返回 |
| | | * @throws Exception 异常 |
| | | * @param bs |
| | | * @param index |
| | | * @param len |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static boolean bytesIsAll0xFF(byte[] bs, int index, int len)throws Exception { |
| | | int count = 0 ; |
| | |
| | | count++ ; |
| | | } |
| | | } |
| | | return count == len; |
| | | return count==len?true:false ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 二进制转十进制数 |
| | | * @param str |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static int binary2Int(String str) throws Exception { |
| | | int cnt=0; |
| | |
| | | return sum; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 字节转存二进制 |
| | | * |
| | | * |
| | | * @param b byte |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | | * @throws Exception |
| | | * @return String |
| | | */ |
| | | public static String byte2Binary(byte b) throws Exception { |
| | | int n = (b + 256) % 256 + 256; |
| | |
| | | } |
| | | /** |
| | | * 字节转存8位二进制 |
| | | * |
| | | * |
| | | * @param b |
| | | * byte |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | | * @throws Exception |
| | | * @return String |
| | | */ |
| | | public static String byte2bit8Binary(byte b) throws Exception { |
| | | String s = byte2Binary(b); |
| | |
| | | } |
| | | return s; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 字节取bit |
| | | * @param b |
| | | * @param b |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | | * @param index |
| | | * @throws Exception |
| | | * @return String |
| | | */ |
| | | public static byte[] getBit(byte b) throws Exception { |
| | | byte[] bs = new byte[8] ; |
| | |
| | | /** |
| | | * 字节取bit |
| | | * @param b |
| | | * @param index 下标位 |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | | * @param index |
| | | * @throws Exception |
| | | * @return String |
| | | */ |
| | | public static byte getBit(byte b, byte index) throws Exception { |
| | | if(index == 0){ |
| | |
| | | |
| | | /** |
| | | * 一个字节转正整数 |
| | | * |
| | | * |
| | | * @param b |
| | | * byte |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | | * @throws Exception |
| | | * @return String |
| | | */ |
| | | public static Short byte2PlusInt(byte b) throws Exception { |
| | | short v = b ; |
| | |
| | | return v ; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * double转换byte |
| | | * @param bs 字节数组 byte[] |
| | | * @param bs byte[] |
| | | * @param value double double类型的参数 |
| | | * @param from 开始位 int |
| | | * @param from int |
| | | */ |
| | | public static void double2Bytes_BE(byte[] bs, double value, int from)throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * double转换byte,字节顺序是倒的 |
| | | * @param bs 字节数组 byte[] |
| | | * @param bs byte[] |
| | | * @param value double double类型的参数 |
| | | * @param from 开始位 int |
| | | * @param from int |
| | | */ |
| | | public static void double2Bytes_LE(byte[] bs, double value, int from)throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * byte转换double |
| | | * @param bs 字节数组 byte[] |
| | | * @param from 开始位 int |
| | | * @param bs byte[] |
| | | * @param from int |
| | | */ |
| | | public static double bytes2Double_BE(byte[] bs, int from) throws Exception { |
| | | long l = bytes2Long_BE(bs, from); |
| | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * byte转换double,字节顺序是倒的 |
| | | * @param bs 字节数组 byte[] |
| | | * @param from 开始位 int |
| | | * @param bs byte[] |
| | | * @param from int |
| | | */ |
| | | public static double bytes2Double_LE(byte[] bs, int from) throws Exception { |
| | | long l = bytes2Long_LE(bs, from); |
| | |
| | | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 转换long型为byte数组 |
| | | * 转换long型为byte数组 |
| | | * @value bs byte[] |
| | | * @value value long |
| | | * @value from int |
| | |
| | | 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; |
| | | bs[from + i] = Long.valueOf(value & 0xff).byteValue(); |
| | | value = value >> 8; |
| | | if(value == 0){ |
| | | break ; |
| | | } |
| | |
| | | throw new Exception("long2Bytes时数组越界"); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 8位字节数组转换为长整型 |
| | | * @param bs 字节数组 byte[] |
| | | * @return 返回 |
| | | * @param bs byte[] |
| | | * @return |
| | | */ |
| | | public static long bytes2Long_BE(byte[] bs) { |
| | | int len = bs.length ; |
| | |
| | | l = l | ls[i] ; |
| | | } |
| | | return l; |
| | | } |
| | | } |
| | | return 0L ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 8位字节数组转换为长整型 |
| | | * @param bs 字节数组 byte[] |
| | | * @param from 开始位 int |
| | | * @return 返回 |
| | | * @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); |
| | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 8位字节数组转换为长整型 |
| | | * @param bs 字节数组 byte[] |
| | | * @param from 开始位 int |
| | | * @param bs byte[] |
| | | * @param from int |
| | | * @param end int |
| | | * @return 返回 |
| | | * @return |
| | | */ |
| | | public static long bytes2Long_BE(byte[] bs, int from, int end) throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, end); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 8位字节数组转换为长整型 |
| | | * @param bs 字节数组 byte[] |
| | | * @return 返回 |
| | | * @param bs byte[] |
| | | * @return |
| | | */ |
| | | public static long bytes2Long_LE(byte[] bs) { |
| | | int len = bs.length ; |
| | |
| | | l = l | ls[i] ; |
| | | } |
| | | return l; |
| | | } |
| | | } |
| | | return 0L ; |
| | | } |
| | | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 8位字节数组转换为长整型 |
| | | * @param bs 字节数组 byte[] |
| | | * @param from 开始位 int |
| | | * @return 返回 |
| | | * 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); |
| | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 8位字节数组转换为长整型 |
| | | * @param bs 字节数组 byte[] |
| | | * @param from 开始位 int |
| | | * @param bs byte[] |
| | | * @param from int |
| | | * @param end int |
| | | * @return 返回 |
| | | * @return |
| | | */ |
| | | public static long bytes2Long_LE(byte[] bs, int from, int end) throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, end); |
| | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 4位字节数组转换为整型 |
| | | * @param bs 字节数组 |
| | | * @param from 开始位 |
| | | * @return 返回 |
| | | * @param b |
| | | * @return |
| | | */ |
| | | public static int bytes2Int_BE(byte[] bs, int from) throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); |
| | |
| | | throw new Exception("byte2Int时数组越界"); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 4位字节数组转换为整型,字节顺序是倒的 |
| | | * @param bs 字节数组 字节数组 |
| | | * @return 返回 |
| | | * @param b |
| | | * @return |
| | | */ |
| | | public static int bytes2Int_LE(byte[] bs, int from) throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 4); |
| | |
| | | throw new Exception("byte2Int时数组越界"); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 2位字节数组转换为短整型 |
| | | * @param bs 字节数组 字节数组 |
| | | * @return 返回 |
| | | * @param b |
| | | * @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] ; |
| | | int s0 = bs[from + 0] ; |
| | | int s1 = bs[from + 1] ; |
| | | |
| | | // s1不变 |
| | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 2位字节数组转换为短整型,字节顺序是倒的 |
| | | * @param bs 字节数组 字节数组 |
| | | * @return 返回 |
| | | * @param b |
| | | * @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] ; |
| | | int s0 = bs[from + 0] ; |
| | | int s1 = bs[from + 1] ; |
| | | |
| | | // s0不变 |
| | |
| | | } |
| | | /** |
| | | * 字符到一字节转换 |
| | | * |
| | | * |
| | | * @value bs byte[] |
| | | * @value ch char char类型的参数 |
| | | * @value index int |
| | | * @return 返回 |
| | | * @return |
| | | */ |
| | | public static void char2Bytes(byte[] bs, char ch, int index)throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, index); |
| | |
| | | |
| | | /** |
| | | * 一字节转换为字符 |
| | | * @param bs 字节数组 字节数组 |
| | | * |
| | | * @param b |
| | | * @value index int |
| | | * @return 返回 |
| | | * @return |
| | | */ |
| | | public static char bytes2Char(byte[] bs, int index) throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, index); |
| | |
| | | |
| | | /** |
| | | * 字符串型数字转成byte |
| | | * |
| | | * |
| | | * @param s |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static byte string2byte(String s) throws Exception { |
| | | int n ; |
| | | int n = 0; |
| | | try { |
| | | n = Integer.parseInt(s); |
| | | } catch (Exception e) { |
| | |
| | | * @value bs byte[] |
| | | * @value str String |
| | | * @value from int |
| | | * @return 返回 |
| | | * @return |
| | | * @throws java.io.UnsupportedEncodingException |
| | | */ |
| | | public static int string2Bytes_BE(byte[] bs, String str, int from, int end)throws Exception { |
| | |
| | | * @value bs byte[] |
| | | * @value str String |
| | | * @value from int |
| | | * @return 返回 |
| | | * @return |
| | | * @throws java.io.UnsupportedEncodingException |
| | | */ |
| | | public static int string2Bytes_LE(byte[] bs, String str, int from, int end)throws Exception { |
| | |
| | | * @value bs byte[] |
| | | * @value str String |
| | | * @value from int |
| | | * @return 返回 |
| | | * @return |
| | | * @throws java.io.UnsupportedEncodingException |
| | | */ |
| | | public static int string2Bytes_BE(byte[] bs, String str, int from)throws Exception { |
| | |
| | | * @value bs byte[] |
| | | * @value str String |
| | | * @value from int |
| | | * @return 返回 |
| | | * @return |
| | | * @throws java.io.UnsupportedEncodingException |
| | | */ |
| | | public static int string2Bytes_LE(byte[] bs, String str, int from)throws Exception { |
| | |
| | | |
| | | /** |
| | | * 判断数组下标是否越界 |
| | | * |
| | | * |
| | | * @value bsLength 数组总长度 |
| | | * @value toSite 数组偏移量 |
| | | * @return 返回 |
| | | * @return |
| | | */ |
| | | private static boolean isOutOfArrLength(int bsLength, int toSite) { |
| | | if (bsLength > toSite) { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 字节数组转换成十六进制的字符串 |
| | | * |
| | | * @param src byte[] |
| | | * 字节数组转换成十六进制的字符串 |
| | | * |
| | | * @param b byte[] |
| | | * @param hasBlank 16进制是否用空格分隔 |
| | | * @return 返回 String |
| | | * @return String |
| | | */ |
| | | public static String bytes2Hex(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) { |
| | | public static String bytes2Hex(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); |
| | | stringBuilder.append(str); |
| | | } else { |
| | | stringBuilder.append(" " + str); |
| | | stringBuilder.append(" " + str); |
| | | } |
| | | } else { |
| | | stringBuilder.append(str); |
| | | stringBuilder.append(str); |
| | | } |
| | | } |
| | | } |
| | | return stringBuilder.toString().toUpperCase(Locale.US); |
| | | } |
| | | } |
| | | /** |
| | | * 字节数组转换成十六进制的字符串 |
| | | * |
| | | * @param src byte[] |
| | | * 字节数组转换成十六进制的字符串 |
| | | * |
| | | * @param b byte[] |
| | | * @param hasBlank 16进制是否用空格分隔 |
| | | * @param from 开始位 |
| | | * @param len 长度 |
| | | * @return 返回 String |
| | | * @param from |
| | | * @param len |
| | | * @return String |
| | | */ |
| | | public static String bytes2Hex(byte[] src, boolean hasBlank, int from, int len){ |
| | | if (src == null || src.length <= 0 || src.length < from + len) { |
| | | return null; |
| | | } |
| | | public static String bytes2Hex(byte[] src, boolean hasBlank, int from, int len){ |
| | | if (src == null || src.length <= 0 || src.length < from + len) { |
| | | return null; |
| | | } |
| | | byte[] bb = new byte[len]; |
| | | for (int i = 0 ; i < len; i++) { |
| | | bb[i] = src[from + i]; |
| | | } |
| | | return bytes2Hex(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 hexString 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])); |
| | | * @param hexString 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 ; |
| | | } |
| | | return fromIndex ; |
| | | } |
| | | |
| | | 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 返回 转换后的字符串 |
| | | * @return 转换后的字符串 |
| | | */ |
| | | public static String bytesToHex(byte[] bytes) { |
| | | //一个byte为8位,可用两个十六进制位标识 |
| | |
| | | * 将16进制字符串转换为byte[] |
| | | * |
| | | * @param str 待转换字符串 |
| | | * @return 返回 转换后的byte[] |
| | | * @return 转换后的byte[] |
| | | */ |
| | | public static byte[] hexToBytes(String str) { |
| | | if (str == null || "".equals(str.trim())) { |
| | |
| | | /** |
| | | * Convert char to byte |
| | | * @param c char |
| | | * @return 返回 byte |
| | | * @return byte |
| | | */ |
| | | private static byte charToByte(char c) { |
| | | return (byte) "0123456789ABCDEF".indexOf(c); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 整形转成BCD编码 |
| | | * @param i |
| | | * @return 返回 |
| | | * @param l |
| | | * @return |
| | | */ |
| | | public static byte[] int2BCD_BE(int i)throws Exception { |
| | | String str = "" + i; |
| | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 整形转成BCD编码,字节顺序是倒的 |
| | | * @param i |
| | | * @return 返回 |
| | | * @param l |
| | | * @return |
| | | */ |
| | | public static byte[] int2BCD_LE(int i)throws Exception { |
| | | String str = "" + i; |
| | |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 长整形转成BCD编码 |
| | | * @param l |
| | | * @return 返回 |
| | | * @return |
| | | */ |
| | | public static byte[] long2BCD_BE(long l)throws Exception { |
| | | String str = "" + l; |
| | |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 长整形转成BCD编码,字节顺序是倒的 |
| | | * @param l |
| | | * @return 返回 |
| | | * @return |
| | | */ |
| | | public static byte[] long2BCD_LE(long l) throws Exception { |
| | | String str = "" + l; |
| | |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 字符串型数字转成BCD编码 |
| | | * @param s |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static byte[] string2BCD_BE(String s) throws Exception { |
| | | byte[] b ; |
| | | byte[] b = null; |
| | | 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 异常 异常 |
| | | * @return |
| | | * @throws java.io.Exception |
| | | */ |
| | | public static int string2BCD_BE(byte[] bs, String str, int fromIndex)throws Exception { |
| | | byte[] bb = string2BCD_BE(str); |
| | |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 字符串型数字转成BCD编码,字节顺序是倒的 |
| | | * @param s |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static byte[] string2BCD_LE(String s) throws Exception { |
| | | byte[] b = null; |
| | |
| | | |
| | | return b; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * 字符串转换成byte数组 |
| | | * @value bs byte[] |
| | | * @value str String |
| | | * @value fromIndex int |
| | | * @return 返回 |
| | | * @throws Exception 异常 异常 |
| | | * @return |
| | | * @throws java.io.Exception |
| | | */ |
| | | public static int string2BCD_LE(byte[] bs, String str, int fromIndex)throws Exception { |
| | | byte[] bb = string2BCD_LE(str); |
| | |
| | | * 大端模式《数据低位在数组高字节》 |
| | | * BCD编码转成整型 |
| | | * @param b |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @param startIndex |
| | | * @param endIndex |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static int BCD2Int_BE(byte b) throws Exception { |
| | | String str = ""; |
| | |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * BCD编码转成整型,字节顺序是倒的 |
| | | * @param b |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @param startIndex |
| | | * @param endIndex |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static int BCD2Int_LE(byte b) throws Exception { |
| | | String str = ""; |
| | |
| | | * @param b |
| | | * @param startIndex |
| | | * @param endIndex |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static int BCD2Int_BE(byte[] b, int startIndex, int endIndex)throws Exception { |
| | | String str = ""; |
| | |
| | | * @param b |
| | | * @param startIndex |
| | | * @param endIndex |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static int BCD2Int_LE(byte[] b, int startIndex, int endIndex)throws Exception { |
| | | String str = ""; |
| | |
| | | * @param b |
| | | * @param startIndex |
| | | * @param endIndex |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static long BCD2Long_BE(byte[] b, int startIndex, int endIndex)throws Exception { |
| | | String str = ""; |
| | |
| | | * @param b |
| | | * @param startIndex |
| | | * @param endIndex |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static long BCD2Long_LE(byte[] b, int startIndex, int endIndex)throws Exception { |
| | | String str = ""; |
| | |
| | | * @param b |
| | | * @param startIndex |
| | | * @param endIndex |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String BCD2String_BE(byte[] b, int startIndex, int endIndex) throws Exception { |
| | | return decodeBCD_BE(b, startIndex, endIndex - startIndex + 1); |
| | |
| | | * @param b |
| | | * @param startIndex |
| | | * @param endIndex |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | * @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,顺序是正的 |
| | | * 编码BCD,例如1387编码成 13 87,顺序是正的 |
| | | * @param value |
| | | * @param dest |
| | | * @param startIndex |
| | | * @param length 长度 |
| | | * @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--) { |
| | |
| | | dest[i] = (byte) (tmpInts[2 * j] * 16 + tmpInts[2 * j + 1]); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 编码BCD,例如1387编码成 87 13,顺序是倒的 |
| | | * 编码BCD,例如1387编码成 87 13,顺序是倒的 |
| | | * @param value |
| | | * @param dest |
| | | * @param startIndex |
| | | * @param length 长度 |
| | | * @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--) { |
| | |
| | | * 解码BCD,顺序是正的 |
| | | * @param src |
| | | * @param startIndex |
| | | * @param length 长度 |
| | | * @return 返回 |
| | | * @param length |
| | | * @return |
| | | */ |
| | | private static String decodeBCD_BE(byte[] src, int startIndex, int length)throws Exception { |
| | | StringBuilder sb = new StringBuilder(); |
| | |
| | | * 编码BCD,顺序是倒的 |
| | | * @param src |
| | | * @param startIndex |
| | | * @param length 长度 |
| | | * @return 返回 |
| | | * @param length |
| | | * @return |
| | | */ |
| | | private static String decodeBCD_LE(byte[] src, int startIndex, int length)throws Exception { |
| | | StringBuilder sb = new StringBuilder(); |