how to convert the byte array into an integer array

前端 未结 2 1996
挽巷
挽巷 2020-12-10 15:35

I am working on a project in which I am receiving image data as an array of bytes (1-byte per pixel). Each byte represents a greyscale integer (0-255).

In order to

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 16:14

    It depends on the use of the resulted int array, but normally, converting a byte array or ByteBuffer into an integer array means "wraping" 4 bytes into 1 integer, e.g. for bitmaps, so in that case I would suggest the following conversion:

    IntBuffer ib = ByteBuffer.wrap(input).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
    int[] ret = new int[ib.capacity()];
    ib.get(ret);
    return ret;
    

提交回复
热议问题