| | |
| | | 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时数组越界"); |
| | |
| | | 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 ; |
| | | } |
| | | |
| | | |
| | | |
| | | private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| | | // /** |
| | | // * 将byte[]转换为16进制字符串 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 小端模式《数据低位在数组低字节》 |
| | | * 整形转成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 |