I have a byte array which i am passing to a function nibbleSwap. nibbleSwap swaps the 1st 4 bits with the 2nd 4 bits for each byte value in the array. After swapping i have to return the swapped byte values. If i just print the byte array i am able to get the correct value but when i return the swapped byte array, it does not print the correct value.
My code is like this:
private static byte[] nibbleSwap(byte []inByte){
int []nibble0 = new int[inByte.length];
int []nibble1 = new int[inByte.length];
byte []b = new byte[inByte.length];
for(int i=0;i<inByte.length;i++)
{
nibble0[i] = (inByte[i] << 4) & 0xf0;
nibble1[i] = (inByte[i] >>> 4) & 0x0f;
b[i] =(byte) ((nibble0[i] | nibble1[i]));
/*System.out.printf(" swa%x ",b[i]); --- if i do this by changing the return to void i get the correct output.
*/
}
return b;
}
eg. valuebyte[] contains: 91,19,38,14,47,21,11
I want the function to return an array containing 19,91,83,41,74,12,11. Also, can i return this as a String by changing the return type as String because when i did that and printed it, i got the integer values of the swapped byte values?
Please Help!!
Pranay
The code does exactly what you say you want it to do, on your test data, provided of course that the values (91, 19, 38, 14, 47, 21, 11) are hexadecimal (0x91, 0x19, and so on).
I used the following code to call your function:
This prints out:
To return the result as a string, you could use something along the following lines: