I read byte[] from socket as Param_Code. there is a condition for Param_Code as below:
• Option 1: Sometimes the Param_Code corresponds to the ID
• Option 2: Sometimes the Param_Code corresponds to 0x40000000 + the ID
• Option 3: Sometimes the Param_Code corresponds to 0x80000000 + the ID
This is my code:
byte[] cbuf = new byte[4];
socketReader.read(cbuf, 0, 4);
int Param_Code = byteArrayToIntBI(cbuf, 0);
public static int byteArrayToIntBI(byte[] b, int offset) {
int value = 0;
for (int i = 3; i > -1; i--) {
int shift = (i) * 8;
value += (b[i + offset] & 0x000000FF) << shift;
}
return value;
}
My question is, How can i know (with if statement or something else) when the Param_Code is one of above option?
How can i get ID?
if( ? ){
it is Option 1 and ID = ?
}else if( ? ){
it is Option 1 and ID = ?
}else if( ? ){
it is Option 1 and ID = ?
}
You should add some restriction on ID. Because we cannot apply mask in that case.
Imagine, that you got 0x81234123. It can be:
Provide additional condition to avoid that ambiguity.
i can only assume that your ID doesn’t include first hexadecimal number so, in that case use mask:
ID = NUM & 0x0FFFFFFFNUM is actually your byte[4]