try {
new BitSet();
byte[] packetData = new byte[receivePacket.getLength()];
packetData = receivePacket.getData();
bits = BitSet.valueOf(packetData);
byte_header = Arrays.copyOfRange(packetData,0,12);
byte_header_id = Arrays.copyOfRange(byte_header,0,2);
byte_header_flags = Arrays.copyOfRange(byte_header,2,4);
BitSet bits_packetData = BitSet.valueOf(receivePacket.getData());
BitSet bits_header = BitSet.valueOf(byte_header);
BitSet bits_header_flags = BitSet.valueOf(byte_header_flags);
System.out.println("packetData length: " + receivePacket.getLength());
System.out.println("byte_header length: " + byte_header.length);
System.out.println("byte_header_flags length: " + byte_header_flags.length);
System.out.println("bits_packetData length: " + bits_packetData.size());
System.out.println("bits_header length: " + bits_header.size());
System.out.println("bits_header_flags length: " + bits_header_flags.size());
} catch (Exception ex) { ex.printStackTrace(); }
背景:以上是从发送给客户端的UDP数据包中获取数据,并从数据包中提取数据。看似异常的输出如下:
packetData 长度:44
byte_header 长度:12
byte_header_flags 长度:2
bits_packetData 长度:384
bits_header 长度:64
bits_header_flags 长度:64
这里唯一看起来正确的数字是 packetData 长度、byte_header_length 和 byte_header_flags_length。packetData bytes * 8 bits 不等于 384。 byte_header_length * 8 bits 不等于 64, byte_header_flags_length * 8 bits 肯定不等于 64!!
谁能澄清我做错了什么,并指出我正确的方向?
谢谢!