//接收1:将InputStream先转为byte【】, public static final byte[] input2byte(InputStream inStream) throws IOException { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] in2b = swapStream.toByteArray(); return in2b; } //接收2:将byte【】转为16进制 public static String bytesToHexString(byte[] src) { StringBuffer sb = new StringBuffer(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v).toUpperCase(); if (hv.length() < 2) { sb.append(0); } sb.append(hv); if (i != src.length - 1) { sb.append(" "); } } return sb.toString(); } //解析:将16进制的字符串 hex转ascii字符串 public static String hexStr2Str(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); } return new String(bytes); } //将hex string 转为byte 客户端经过TCP会收到和hexstring 同样的16进制 private static byte[] hexStringToHex(String str) { byte[] result = new byte[18]; String tempString=""; String[] ss = str.split(" "); for (int i = 0;i < ss.length;i++){ result[i]= (byte) Integer.parseInt(ss[i], 16); // System.out.println(ss[i]); } System.out.println(Arrays.toString(result)); return result; }