简体   繁体   English

如何将所有字节发送到单个数组中,然后再在字节之间进行异或运算?

[英]How to send all byte into single array and later xor between the bytes?

I have socket application and I can read byte by byte and I need push all the byte into one single array. 我有套接字应用程序,我可以逐字节读取字节,并且需要将所有字节放入一个数组中。 I read like below.So I will have 12+bodylen bytes. 我读如下,所以我将有12 + bodylen个字节。

int messageID = r.readUnsignedShort();
int bodyLen = r.readUnsignedShort();       
byte[] phoneNum = new byte[6];
r.readFully(phoneNum);  
int serialNum = r.readUnsignedShort();     
byte[] messageBody = new byte[bodyLen];    
r.readFully(messageBody);
byte checkCode = r.readByte(); 

My challenge is how to push all the byte into one fullMessage and there after I need to xor between each of this bytes and get the results as byte too. 我的挑战是如何将所有字节放入一个fullMessage中,然后在我需要在每个字节之间进行异或运算并将结果也作为字节获得之后。

byte[] fullMessage = new byte[12+bodyLen];

If you dont need to have everything in separate and you need all in single array, after reading bodyLen you can do something like this: 如果您不需要将所有内容都分开并且需要将所有内容都放在一个数组中,那么在阅读bodyLen您可以执行以下操作:

int messageID = r.readUnsignedShort();
int bodyLen = r.readUnsignedShort();       
byte[] fullMessage=new byte[12+bodyLen];
r.readFully(fullMessage,0,fullMessage.length);

Here fullMessage will contain all the data you are reading step by step in your code in single array. 在这里, fullMessage将在单个数组中包含您逐步读取的所有数据。

But if you need all the parts to be read separately: read below 但是,如果需要分别阅读所有部分,请阅读以下内容

To concatenate arrays and other elements into array use ByteBuffer#put(byte[]) . 要将数组和其他元素连接到数组中,请使用ByteBuffer#put(byte []) After work is done, get the array from buffer using .array() method of ByteBuffer 完成工作后,使用ByteBuffer .array()方法从缓冲区中获取数组。

byte[] fullMessage=byteBuffer.array();

Later on, iterate over array and do required work 稍后,遍历数组并完成所需的工作

for(int i=0,s=fullMessage.lengthl;i<s;i++){
     // do your XOR operations -> xor operator is ^
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM