This question may seems unique but I am having difficulties on solving it.
I have a byte array (a DW per row) and trying to get the byte value for every 9 bytes. Here is how the byte array looks like:
0 1 2 3
4 5 6 7
8| 9 10 11
12 13 14 15
16 17| 18 19
20 21 22 23
24 25 26| 27
28 29 30 31
32 33 34 35|
.......
Here is a defined function that I need to use to get the value from the above patent:
getValue(int DWOffset, int highBit, int lowBit)
//DWOffset indicates the row of the data.
//highBit indicates the ending bit
//lowBit indicates the starting bit
My question is how to get every 9 bytes from the data (where the | appears) by using for loop? The data length can be found out by using
data.getLength(); //which returns the total number of bytes in the data
So far, I can get value on the first two row:
for(int i = 0; i < data.getLength()/(4*2); i++){
getValue(i*2, 31, 0); //DW i
getValue(i*2 + 1, 31, 0); //DW i+1
}
Part of the problem is that you seem to be mixing paradigms, conceptually looking at it as both a 2-dimensional and a 1-dimensional array. Looking at it as 2-dimensional, you need a getValue(int row, int column) that returns a single byte. Then you can do something like this:
final int TOTAL_BYTES_TO_INCLUDE = 9;
byte[] byteArray[] = new byte[TOTAL_BYTES_TO_INCLUDE];
int arrayIndex = 0;
for (int r = 0; r++; r < number_of_rows) {
for (int c = 0; c++; c < 4) {
byteArray[arrayIndex++] = getValue(r, c);
if (arrayIndex >= TOTAL_BYTES_TO_INCLUDE) {
... do something with your byte array ...
arrayIndex = 0;
}
}
}
There's some corner cases to deal with, like what happens if the number of available values is not a multiple of 9, what happens. What happens if the value read in causes byte-overflow. I don't reinitialize the array before each iteration, but is there any risk to something being left behind.
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.