Need help understanding the int array byte array method and Little / Big Endian?

Take a couple of questions, first on this method, which converts int[]

to byte[]

:

public static byte[] intToByte(int[] input){
    ByteBuffer byteBuffer = ByteBuffer.allocate(input.length * 4);
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(input);
    byte[] array = byteBuffer.array();
    return array;
}

      

I am creating a game and I need to send a byte array over a socket and I like this method because it works mostly, but I don't like to use anything I really don't understand what it does, so you could give me some understanding of what this method does? I believe it creates enough room for bits at first, but why does it do "times" four lengths? And is intBuffer connected to byteBuffer? because what you need all the Intbuffer stuff for if not.

Ok, last question, what's the deal with BIG_ENDIAN

vs. LITTLE_ENDIAN

? For example, in my other method that converts a byte array to an int array, what are the benefits of inclusion .order(BIG_ENDIAN)

?

public static int[] byteToInt(byte[] input){
   IntBuffer intBuf = ByteBuffer.wrap(input).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
   int[] array = new int[intBuf.remaining()];
   intBuf.get(array);
   return array;
}

      

I know that BIG_ENDIAN

and LITTLE_ENDIAN

just define how bytes are ordered, but why even define endian? why not just that?

IntBuffer intBuf = ByteBuffer.wrap(input).asIntBuffer();

      

+3


source to share


2 answers


An int

takes four bytes, so you need to allocate byte[]

four times that int[]

. asIntBuffer()

returns the view ByteBuffer

as IntBuffer

, so placing int[]

in IntBuffer

converts everything int

to four bytes each and puts them in ByteBuffer

.

Endianness determines what order the four bytes are int

written to ByteBuffer

.



As for your last question, is IntBuffer

not int[]

, and IntBuffer

derived from ByteBuffer.asIntBuffer()

, does not support the method array()

. In other words, IntBuffer

there is no support for int[]

, since the implementation is based on byte[]

.

+2


source


IntBuffer is an int representation of a byteArray byte array. The purpose of using IntBuffer is its put (int []), which allows you to enter an array of int in one go. Actually you could do without IntBuffer like

    ByteBuffer byteBuffer = ByteBuffer.allocate(input.length * 4);
    for(int i : input) {
        byteBuffer.putInt(i);
    }
...

      

the result will be the same.

This demonstrates the difference between BigEndian (default) and LittleEndian

        int[] input = {1,2,3,4};
        ByteBuffer byteBuffer = ByteBuffer.allocate(input.length * 4);
//      byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        for(int i : input) {
            byteBuffer.putInt(i);
        }
        byte[] array = byteBuffer.array();
        System.out.println(Arrays.toString(array));

      



Output

[0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4]

      

uncomment byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

and the output will be

[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]

      

+2


source







All Articles