| | |
| | | |
| | | /** |
| | | * 截取指定长度的字符串,不足以0填充前面 |
| | | * @param hex 字符串 |
| | | * |
| | | * @param hex 字符串 |
| | | * @param totalBit 截取长度 |
| | | * @return 截取后的字符串 |
| | | */ |
| | |
| | | return hex; |
| | | } |
| | | |
| | | /** 十六进制转换成字节数组 */ |
| | | /** |
| | | * 十进制转16进制,补位0 |
| | | * |
| | | * @param index 字符串 |
| | | * @param totalBit 截取长度 |
| | | * @return 截取后的字符串 |
| | | */ |
| | | public static String suppleZero(Integer index, Integer totalBit) { |
| | | String format = String.format("%02X", index); |
| | | |
| | | totalBit = totalBit - format.length(); |
| | | |
| | | StringBuilder hexBuilder = new StringBuilder(format); |
| | | for (int i = 0; i < totalBit; i++) { |
| | | hexBuilder.insert(0, "0"); |
| | | } |
| | | format = hexBuilder.toString(); |
| | | return format; |
| | | } |
| | | |
| | | /** |
| | | * 十六进制转换成字节数组 |
| | | */ |
| | | public static byte[] hexStringToBytes(String hexString) { |
| | | if (hexString == null || hexString.equals("")) { |
| | | return null; |
| | |
| | | return d; |
| | | } |
| | | |
| | | /** char转byte */ |
| | | 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.toLowerCase()); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 字符串转换为16进制字符串 |
| | | * |
| | | * @param s |
| | | * @return |
| | | */ |
| | | public static String stringToHexString(String s) { |
| | | String str = ""; |
| | | for (int i = 0; i < s.length(); i++) { |
| | | int ch = s.charAt(i); |
| | | String s4 = Integer.toHexString(ch); |
| | | str = str + s4; |
| | | } |
| | | return str; |
| | | } |
| | | |
| | | /** |
| | | * char转byte |
| | | */ |
| | | private static byte charToByte(char c) { |
| | | return (byte) "0123456789ABCDEF".indexOf(c); |
| | | } |
| | | |
| | | |
| | | public static boolean isHex(String str) { |
| | | boolean isHexFlg = true; |
| | | int i = 0; |
| | | char c; |
| | | for (i = 0; i < str.length(); i++) { |
| | | c = str.charAt(i); |
| | | if (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')) || (c == ' '))) { |
| | | isHexFlg = false; |
| | | break; |
| | | } |
| | | } |
| | | return isHexFlg; |
| | | } |
| | | |
| | | } |