简体   繁体   中英

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.

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.

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:

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.

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[]) . After work is done, get the array from buffer using .array() method of ByteBuffer

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 ^
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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