//接收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;
}