| | |
| | | for (int i = 0; i < byteList.size(); i++) { |
| | | byteArray[i] = byteList.get(i); |
| | | } |
| | | String hex = bytesToHexNoAppen(byteArray); |
| | | String hex = bytesToHex(byteArray); |
| | | return Integer.parseInt(hex, 16); |
| | | } |
| | | |
| | | /** |
| | | * byte 数组转16进制字符串 不加0前缀 |
| | | * |
| | | * @param bytes |
| | | * @return |
| | | */ |
| | | public static String bytesToHexNoAppen(byte[] bytes) { |
| | | StringBuffer sb = new StringBuffer(); |
| | | for (int i = 0; i < bytes.length; i++) { |
| | | String hex = Integer.toHexString(bytes[i] & 0xFF); |
| | | sb.append(hex); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | |
| | | public static int getBcdToInt(byte data) { |
| | | return ((data & 0xF0) >> 4) * 10 + ((data & 0x0F)); |
| | | } |
| | | |
| | | |
| | | public static byte[] getIntToBCD(int number) { |
| | | // 获取整数的字符串表示形式 |
| | | String numberStr = Integer.toString(number); |
| | | |
| | | // 创建一个字节数组以存储BCD表示 |
| | | int len = numberStr.length(); |
| | | byte[] bcd = new byte[(len + 1) / 2]; |
| | | |
| | | int j = 0; |
| | | // 如果数字的长度是奇数,需要在前面添加一个0 |
| | | if (len % 2 != 0) { |
| | | numberStr = "0" + numberStr; // 先添加前导零 |
| | | len++; |
| | | } |
| | | |
| | | // 将每一对数字转换为一个字节 |
| | | for (int i = 0; i < len; i += 2) { |
| | | bcd[j++] = (byte) (((numberStr.charAt(i) - '0') << 4) | (numberStr.charAt(i + 1) - '0')); |
| | | } |
| | | |
| | | return bcd; |
| | | } |
| | | /** |
| | | * short10进制转16进制 低位在前高位在后 |
| | | * |
| | |
| | | for (int i = 0; i < byteList.size(); i++) { |
| | | byteArray[i] = byteList.get(i); |
| | | } |
| | | String hex = bytesToHexNoAppen(byteArray); |
| | | String hex = bytesToHex(byteArray); |
| | | int intValue = Integer.parseInt(hex, 16); // 将十六进制字符串转换为整数表示 |
| | | float floatValue = Float.intBitsToFloat(intValue); // 将整数表示转换为浮点数 |
| | | return floatValue; |
| | |
| | | * @return |
| | | */ |
| | | public static String spaceHex(String str) { |
| | | if (str.length() % 2 != 0) { |
| | | //奇数时前面加0补齐称为偶数 |
| | | str = "0" + str; |
| | | } |
| | | char[] array = str.toCharArray(); |
| | | if (str.length() <= 2) return str; |
| | | StringBuffer bufferHex = new StringBuffer(); |
| | |
| | | * @param str |
| | | * @return |
| | | */ |
| | | private static String HighLowHex(String str) { |
| | | public static String HighLowHex(String str) { |
| | | if (str.trim().length() <= 2) return str; |
| | | List<String> list = Arrays.asList(str.split(" ")); |
| | | Collections.reverse(list); |