how to convert the byte array into an integer array

前端 未结 2 2022
挽巷
挽巷 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:16

    Anything wrong with the simple approach?

    public static int[] convertToIntArray(byte[] input)
    {
        int[] ret = new int[input.length];
        for (int i = 0; i < input.length; i++)
        {
            ret[i] = input[i] & 0xff; // Range 0 to 255, not -128 to 127
        }
        return ret;
    }
    

    EDIT: If you want a range of -128 to 127:

    public static int[] convertToIntArray(byte[] input)
    {
        int[] ret = new int[input.length];
        for (int i = 0; i < input.length; i++)
        {
            ret[i] = input[i];
        }
        return ret;
    }
    

提交回复
热议问题