Search code examples
javacastingarraysunsigned-integer

java: fastest way of converting a bye array byte [1000] to array int[500]


The goal is to convert every pair of bytes as a single unsigned 16bit int. In C I would define an array[500] of 16bit unsinged int pointer and would point it the the array of bytes, but in java I am not aware of such short cut. I know in Java there is no 16bit data type except char, however that is not an issue. We only need to copy every two consecutive two bytes into a single int of an array of integers. So the array of integer holds values of int ranging from 0 to 65535 (2^16-1).


Solution

  • I think that there are no nice tricks in Java like the aliasing you can do in C. You're going to have to do this by hand:

    public int[] pack(byte[] bytes) {
        int n = bytes.length >> 1;
        int[] packed = new int[n];
        for (int i = 0; i < n; ++i) {
            int i2 = i << 1;
            int b1 = bytes[i2] & 0xff;
            int b2 = bytes[i2 + 1] & 0xff;
            packed[i] = (b1 << 8) | b2;
        }
        return packed;
    }
    

    (This can probably be sped up a bit, but probably not worth it for 1,000 elements unless it's being done a lot.) Note that the promotion from byte to int requires a bit of extra work to deal with unwanted sign extension.