1 Reply Latest reply on Jan 10, 2011 4:24 PM by sabre150

# short[] to byte[] conversion

Hi,

Im trying to solve something similar to the FFT problem found by this user (FFT noise reduction problem).

The current method of conversion :
`````` short[] unsignedByteArray;
int[] unsignedShortArray;
shortArray = array;
isBigEndian = bigEndian;
int N = shortArray.length;

unsignedShortArray = new int[N];
for (int i = 0; i < N; i++) {
unsignedShortArray[i] = shortArray[i] + 128 * 257;
}
byteArray = new byte[N * 2];
unsignedByteArray = new short[N * 2];
if (isBigEndian) {
for (int i = 0; i < N; i++) {
unsignedByteArray[i * 2] = (short) (unsignedShortArray[i] / 256);
unsignedByteArray[i * 2 + 1] = (short) (unsignedShortArray[i] % 256);
}
} else {
for (int i = 0; i < N; i++) {
unsignedByteArray[i * 2 + 1] = (short) (unsignedShortArray[i] / 256);
unsignedByteArray[i * 2] = (short) (unsignedShortArray[i] % 256);
}
}
for (int i = 0; i < N * 2; i++) {
byteArray[i] = (byte) (unsignedByteArray[i] - 128);
}``````
ive tried tackling the problem 2 different ways, neither of which remove the noise from the output

attempt 1: More noise than current code
``````        shortArray = array;
int N = shortArray.length;

byte out[] = new byte[N * 2];
for (int i = 0; i < N; i++) {
out[i * 2] = (byte) (array[i] >>> 8);//most significant
out[(i * 2) + 1] = (byte) array;//least significant
}
return out;

attempt 2: less noise than attempt 1, still more than current
shortArray = array;
int N = shortArray.length;
int hexBase = 0xff;

byte out[] = new byte[N * 2];
for (int i = 0; i < N; i++) {
out[i*2] = (byte) (hexBase & array);
out[(i*2)+1] = (byte) ((byte) ((hexBase <<8) & array[i]) >> 8);

}
return out;

I think solving this problem is beyond my capability, but if someone could help in producing a less noisy output im sure many more would benefit from a fully operational FFT implementation ``````
• ###### 1. Re: short[] to byte[] conversion
That all seems very contorted -
``````byte mostSignificantByte = (byte)(theShort >> 8);
byte leastSignificantByte = (byte)theShort;``````