1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | import org.apache.commons.lang3.StringUtils; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.math.BigInteger; public class RadarDecodeUtil { public static void main(String[] args) { System.out.println("大小端转换前=="+"6B6BB23BA3B55F0C"); System.out.println("大小端转换后=="+bigEndianChangeLittleEndian("6B6BB23BA3B55F0C")); } public static String bigEndianChangeLittleEndian(String str){ if(!org.springframework.util.StringUtils.isEmpty(str)){ byte[] bytes1 = hexStrToByteArrs(str); //System.out.println("====="+bytes1.length); byte[] bytes2 = new byte[str.length() / 2]; int index = 0; for (int i = bytes1.length - 1; i>=0; i--){ bytes2[index] = bytes1[i]; index++; } return bytesToHexString(bytes2); } return null; } /** * hexStr 字符串转换为 字符串 * @param hexStr * @return */ public static String hexStrToString(String hexStr){ String str = "0123456789ABCDEF"; char[] hexs = hexStr.toCharArray(); byte[] bytes = new byte[hexStr.length() / 2]; int n; for (int i = 0; i < bytes.length; i++) { n = str.indexOf(hexs[2 * i]) * 16; n += str.indexOf(hexs[2 * i + 1]); bytes[i] = (byte) (n & 0xff); } String returnStr = new String(bytes); return returnStr; } /** * hexStr 转 UnsignedLong * @param hexStr * @return */ public static Long hexStrToUnsignedLong(String hexStr){ if(StringUtils.isEmpty(hexStr)){ return null; } return Long.parseUnsignedLong(hexStr,16); } /** * hexStr 转 Double * @param hexStr * @return */ public static Double hexStrToDouble(String hexStr){ if(StringUtils.isEmpty(hexStr)){ return null; } long longBits = Long.valueOf(hexStr,16).longValue(); return Double.longBitsToDouble(longBits); } /** * hexStr 转 Float * @param hexStr * @return */ public static Float hexStrToFloat(String hexStr){ if(StringUtils.isEmpty(hexStr)){ return null; } Integer integerBits = Integer.valueOf(hexStr,16); return Float.intBitsToFloat(integerBits); } /** * hexStr 转 BigDecimal * @param hexStr * @return */ public static BigDecimal hexStrToBigDecimal(String hexStr){ if(StringUtils.isEmpty(hexStr)){ return null; } BigInteger bigInteger = new BigInteger(hexStr, 16); return new BigDecimal(bigInteger); } /** * hexStr 转 Byte * @param hexStr * @return */ public static Byte hexStrToByte(String hexStr){ if(StringUtils.isEmpty(hexStr)){ return null; } return Byte.parseByte(hexStr,16); } /** * hexStr 转 Integer * @param hexStr * @return */ public static Integer hexStrToInteger(String hexStr){ if(StringUtils.isEmpty(hexStr)){ return null; } return Integer.parseInt(hexStr,16); } /** * hexStr 转 BigInteger * @param hexStr * @return */ public static BigInteger hexStrToBigInteger(String hexStr){ if(StringUtils.isEmpty(hexStr)){ return null; } return new BigInteger(hexStr,16); } /** * 将十六进制的字符串转换成字节数组 * * @param hexString * @return */ public static byte[] hexStrToByteArrs(String hexString) { if (StringUtils.isEmpty(hexString)) { return null; } hexString = hexString.replaceAll(" ", ""); int len = hexString.length(); int index = 0; byte[] bytes = new byte[len / 2]; while (index < len) { String sub = hexString.substring(index, index + 2); bytes[index / 2] = (byte) Integer.parseInt(sub, 16); index += 2; } return bytes; } /** * 数组转换成十六进制字符串 * * @param bArray * @return HexString */ public static final String bytesToHexString(byte[] bArray) { StringBuffer sb = new StringBuffer(bArray.length); String sTemp; for (int i = 0; i < bArray.length; i++) { sTemp = Integer.toHexString(0xFF & bArray[i]); if (sTemp.length() < 2){ sb.append(0); } sb.append(sTemp.toUpperCase()); } return sb.toString(); } } |