简体   繁体   中英

How to convert array of 16-bit signed integers to byte array in Java?

Let's say that on Android (Java) we have a byte array that represents a sequence of 16-bit signed integers.

I am needing to be able to decode the values from this array by using a for-loop and concatenating each pair of bytes to retrieve the original value back again (Please don't suggest a different method such as a ByteBuffer).

In order to test my algorithm, I want to encode and decode a list of ints and see if I get the same numbers back.

However, I can't figure out how to encode the original list of ints into a byte array for testing purposes. I don't want to simply reverse my algorithm because I don't know if it works yet... I would be open to using ByteBuffer or any other known-good means to do the encoding because it's only for testing/simulation purposes -- in the real app, the byte array is already encoded by Android.AudioRecord.

    // dec : short : byte a : byte b
    // 4536 : 0001000100000100 : 17 : 4
    // -1 : 1111111111111111 : -1 : -1
    // -32768 : 1000000000000000 : -128 : 0 
    // 32767 : 1000000000000001 : -128 : 1
    // 0 : 0000000000000000 : 0 : 0
    // -2222 : 1111011101010010 : -9 : 82

void _go() {
        
        int[] source = {4536,-1,-32768,32767,0,-2222};

        // is this even correct?
        byte[] expectedEncoding = {17,4,-1,-1,-128,0,-128,1,0,0,-9,82};

        byte[] encoded = ??? // <----- what goes here?

        int[] decoded = new int[source.length];

        // the algorithm I'm testing
        for (int i=0; i < encoded.length/2; i++) {
            byte a = encoded[i];
            byte b = encoded[i+1];

            decoded[i] = (short) (a<<8 | b & 0xFF);

        }

        Log.i("XXX", "decoded values: " + decoded.toString());
        
    }

Here an exemple...

short[] arrayOfShorts = {6, 1, 2, 5};
byte[] arrayOfBytes = new byte[shortArray.length * 2];
ByteBuffer.wrap(byteArray)
                 .order(ByteOrder.LITTLE_ENDIAN)
                 .asShortBuffer().put(shortArray);

Here is an example of how you can convert an array of 16-bit signed integers to a byte array in Java:

void _go() {
int[] source = {-1,0,32767,-32768,123,456,-999};
ByteBuffer byteBuffer = ByteBuffer.allocate(source.length * 2);
ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
shortBuffer.put(source);
byte[] encoded = byteBuffer.array();

int[] decoded = new int[source.length];

for (int i=0; i < encoded.length/2; i++) {
    byte a = encoded[i*2];
    byte b = encoded[i*2 + 1];

    decoded[i] = (short) ((a & 0xff) << 8 | (b & 0xff));
}

Log.i("XXX", "decoded values: " + Arrays.toString(decoded));

}

Here we first create a ByteBuffer with the length of the source array * 2, since each int is 16 bit long. Then we create a ShortBuffer using the asShortBuffer() method, so we can use the put() method to insert the int array in it. With array() method of the byteBuffer we get the byte array.

Then we loop through the encoded byte array and extract the two bytes at a time, shifting the first byte left by 8 bits and adding the second byte. The result is then casted to short and stored in the decoded array.

Finally we log the decoded array to check if the operation was successful.

Note that this is just an example and you may need to adjust it depending on the specific requirements of your algorithm.

You can convert an array of 16-bit signed integers to a byte array in Java using the following steps:

Here is an example implementation:

  public static byte[] convertToByteArray(short[] shortArray) {
        byte[] byteArray = new byte[shortArray.length * 2];
        ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
        for (short value : shortArray) {
            byteBuffer.putShort(value);
        }
        return byteArray;
    }

Note: This conversion is also known as serialization and deserialization, which can be done by different libraries like Avro, Thrift etc.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM