java socket解析和发送二进制报文工具(附java和C++转化问题)

时间:2021-12-17 11:53:20

解析:

首先是读取字节:

/**
* 读取输入流中指定字节的长度
* <p/>
* 输入流
*
* @param length 指定长度
* @return 指定长度的字节数组
*/
public static byte[] readBytesFromTo(byte[] buffer, int from, int length) {
byte[] sub = new byte[length];
int cur = 0;
for (int i = from; i < length + from; i++) {
sub[cur] = buffer[i];
cur++;
}
return sub;
}

读取之后转为字符串或者整型:

    /**
* byte[]转int
*
* @param bytes 报文
* @return Integer
*/
public static int byteArrayToInt(byte[] bytes) {
int value = 0;
// 由高位到低位
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (bytes[i] & 0x000000FF) << shift;// 往高位游
}
return value;
}
/**
* 字节转字符串
* @param bytes 报文
*/
public static String byteArrayToString(byte[] bytes,int start , int end){
return new String(bytes, start, end);
}

发送报文:

将java类型转化为二进制:

   /**
* @param i
* @return
*/
public static byte[] intToByteArray(int i) {
byte[] result = new byte[4];
result[0] = (byte) ((i >> 24) & 0xFF);
result[1] = (byte) ((i >> 16) & 0xFF);
result[2] = (byte) ((i >> 8) & 0xFF);
result[3] = (byte) (i & 0xFF);
return result;
} /**
*
* @param s
* @return
*/
public static byte[] StringToByteArray(String s) {
return s.getBytes();
}

整合二进制数组:

    /**
*
* @param bytes
* @return
*/
public static byte[] combineByte(byte[] ... bytes){
int length = 0;
for (byte[] b : bytes) {
length+=b.length;
}
byte[] allByte=new byte[length];
int positon=0;
for (byte[] b : bytes) {
System.arraycopy(b, 0, allByte, positon, b.length);
positon+=b.length;
}
return allByte;
}

求校验和:

  /**
*
* @param input
* @return
*/
public static int getCheckSum(byte[]... input) {
int re = 0;
for (byte[] b : input) {
for (byte aB : b) {
re += aB & 0x0FF;//注意此处要转化为无符号
}
}
return re;
}

二进制内容有时候要在不同的环境下解析和发送,下面是C++和java的字符差异

下面给出在不同字符集编码下的字节数:

英文字母:

字节数 : 1;编码:GB2312             字节数: 1;编码:GBK            字节数 : 1;编码:GB18030

字节数 : 1;编码:ISO-8859-1        字节数: 1;编码:UTF-8         字节数 : 4;编码:UTF-16

字节数 : 2;编码:UTF-16BE           字节数: 2;编码:UTF-16LE

中文汉字:

字节数 : 2;编码:GB2312              字节数: 2;编码:GBK            字节数 : 2;编码:GB18030

字节数 : 1;编码:ISO-8859-1        字节数: 3;编码:UTF-8         字节数 : 4;编码:UTF-16

字节数 : 2;编码:UTF-16BE           字节数: 2;编码:UTF-16LE

编码不同会影响解析的方式的差异,有时候还是蛮头疼的,比如我们常用的中文问题,C++默认用GB2312编码,所以java的解析要变一下:

String describe = new String(bytes, start += 4, describeLength, Charset.forName("GB2312"));