So I have been given a C program, which I am trying to make more portable by translating to Java, which has been going well. However, I have run into a bit of hangup, which I believe lies in this bit of code. It just a function that unpacks a 30 byte frame. I have a feeling I am running into issues with the variable types, trying to figure out the best way to sub Java types for some that don’t carry over from C. If anyone sees where the issue may be, I would appreciate it.
Java:
public static byte[] unPackBits(byte[] Src, int bitOffset, byte[] Dst, int bitCount){
int srcByteOffset, srcBit;
int dstByteOffset, dstBit;
char dstMask, srcMask;
srcByteOffset = bitOffset / 8;
srcBit = bitOffset % 8;
srcMask = (char)(0x01<<srcBit);
dstByteOffset = 0;
dstBit = 0;
dstMask = 0x01;
Dst[dstByteOffset] = '\0';
for(int b = 0; b < bitCount; b++){
if((Src[srcByteOffset] & srcMask) != (char)0x00){
Dst[dstByteOffset] = (byte)(Dst[dstByteOffset]|dstMask);
}
else {
Dst[dstByteOffset] = (byte)(Dst[dstByteOffset] & (~dstMask));
}
srcBit++;
if(srcBit < 8) {
srcMask = (char)(srcMask<<1);
}
else {
srcByteOffset++;
srcBit = 0;
srcMask = 0x01;
}
dstBit++;
if(dstBit < 8) {
dstMask = (char)(dstMask<<1);
}
else {
dstByteOffset++;
dstMask = 0x01;
dstBit = 0;
}
}
return Dst;
}
C:
void TRB::unPackBits(char *Src, int BitOffset, char *Dst, int BitCount) {
int srcByteOffset, srcBit;
int dstByteOffset, dstBit;
char dstMask, srcMask;
srcByteOffset = BitOffset / 8;
srcBit = BitOffset % 8;
srcMask = 0x01<<srcBit;
dstByteOffset = 0;
dstBit = 0;
dstMask = 0x01;
Dst[dstByteOffset] = '\0';
for(int b = 0; b < BitCount; b++) {
if((Src[srcByteOffset] & srcMask) != (char)0x00) {
Dst[dstByteOffset] = Dst[dstByteOffset] | dstMask;
}
else {
Dst[dstByteOffset] = Dst[dstByteOffset] & (~dstMask);
}
srcBit++;
if(srcBit < 8) {
srcMask = srcMask<<1;
}
else {
srcByteOffset++;
srcBit = 0;
srcMask = 0x01;
}
dstBit++;
if(dstBit < 8) {
dstMask = dstMask<<1;
}
else {
dstByteOffset++;
dstMask = 0x01;
dstBit = 0;
}
}
}
The problem is that C doesn’t have a built in way to easily access data inside a char *. The unPackBits function is just extracting data from a certain location (BitOffset) with a certain length (BitCount) and returning it in the same form. Then that data has to be cast to the appropriate types, be it bool, int, double, etc.
Java, on the other hand has a built in way to handle the same issue. Data that is stored in a byte array can be accessed by using ByteBuffer.wrap(byte[]). ByteBuffer has functions that return int, double, long, short, float, and char, as well as ones to put all of those types and a few other useful functions, like translating the entire structure into an int array. Each of these only need to be told the offset, they hold in memory the BitCount. The only primitive data type it doesn’t handle is boolean, so I will still be unpacking with the byte array, not a problem since I know exactly where in the array those values are held.
As for the bit shifting, shift the entire array before wrapping it as a ByteBuffer, as suggested by @vidstige, in the same way the first if else in the for loop does, and everything will be in order.