package com.sandu.ximon.admin.utils; import java.util.ArrayList; /** * 16 */ public class HexStrConvertUtil { /** * byte数组转换二进制字符串 **/ public static String HexToByteStr(String hexStr) { String[] strs = new String[]{ "0000","0001","0010","0011", "0100","0101","0110","0111", "1000","1001","1010","1011", "1100","1101","1110","1111" }; String result = ""; for(char c:hexStr.toCharArray()){ result = result + strs["0123456789ABCDEF".indexOf(c)]; } return result; } /** * 字符串转换为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; } /** * 16进制转换成为string类型字符串 * @param s * @return */ public static String hexStringToString(String s) { if (s == null || s.equals("")) { return null; } s = s.replace(" ", ""); byte[] baKeyword = new byte[s.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "UTF-8"); new String(); } catch (Exception e1) { e1.printStackTrace(); } return s; } /** * @param: [content] * @return: int * @description: 十六进制转十进制 */ // public static int HexToInt(String content){ // int number=0; // String [] HighLetter = {"A","B","C","D","E","F"}; // Map map = new HashMap<>(); // for(int i = 0;i <= 9;i++){ // map.put(i+"",i); // } // for(int j= 10;j= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')) || (c == ' '))) { isHexFlg = false; break; } } return isHexFlg; } // public static byte[] readInputStream(InputStream inputStream) { // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // // if (inputStream != null) { // try { // byte[] buffer = new byte[91]; // int counts; // while ((counts = inputStream.read(buffer, 0, buffer.length)) > 0) { // byteArrayOutputStream.write(buffer, 0, counts); // } // } catch (IOException e) { // e.printStackTrace(); // }/* finally { // if (byteArrayOutputStream != null) { // try { // byteArrayOutputStream.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // }*/ // } // return byteArrayOutputStream.toByteArray(); // } // br = new BufferedReader(new InputStreamReader(inputStream)); // byteArrayOutputStream = new ByteArrayOutputStream(); // while ((data = br.readLine()) != null) { // Log.i("HexStrConvertUtil", "readInputStream: " + data); // byte[] ba = HexStrConvertUtil.hexStr2Bytes(data); // Log.i("HexStrConvertUtil", "bytes : " + Arrays.toString(ba)); // } /** * 合并数组 * * @param firstArray 第一个数组 * @param secondArray 第二个数组 * @return 合并后的数组 */ public static byte[] connect(byte[] firstArray, byte[] secondArray) { if (firstArray == null || secondArray == null) { return null; } byte[] bytes = new byte[firstArray.length + secondArray.length]; System.arraycopy(firstArray, 0, bytes, 0, firstArray.length); System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length); return bytes; } //合并数组 public static byte[] connectByte(byte[] firstArray, byte[] secondArray) { if (firstArray == null || secondArray == null) { return null; } byte[] bytes = new byte[firstArray.length + secondArray.length]; System.arraycopy(secondArray,0,bytes,firstArray.length,secondArray.length); // System.arraycopy(firstArray,0,secondArray,secondArray.length,bytes.length); // System.arraycopy(firstArray, 0, bytes, 0, firstArray.length); // System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length); return bytes; } }