Java Convert via ByteBuffer toIntArray(final byte[] byteArray)

Here you can find the source of toIntArray(final byte[] byteArray)

Description

to Int Array

License

Open Source License

Declaration

public static int[] toIntArray(final byte[] byteArray) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static int[] toIntArray(final byte[] byteArray) {
        if (byteArray.length % 2 != 0) {
            throw new IllegalArgumentException(
                    "Cannot convert an odd sized byte array into an int array.");
        }// w w w  .ja  va  2s . c  o  m
        int[] intArray = new int[byteArray.length / 2];
        for (int i = 0; i < byteArray.length; i += 2) {
            intArray[i / 2] = toInt(byteArray[i], byteArray[i + 1]);
        }
        return intArray;
    }

    public static int toInt(final byte lowOrderByte,
            final byte highOrderByte) {
        return toInt(lowOrderByte, highOrderByte, ByteOrder.LITTLE_ENDIAN);
    }

    public static int toInt(final byte lowOrderByte,
            final byte highOrderByte, final ByteOrder byteOrder) {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4);
        byteBuffer.order(byteOrder);
        byteBuffer.put(lowOrderByte);
        byteBuffer.put(highOrderByte);
        byteBuffer.put((byte) 0x00);
        byteBuffer.put((byte) 0x00);
        byteBuffer.flip();
        return byteBuffer.getInt();
    }
}

Related

  1. toInt(byte[] value)
  2. toInt(final byte lowOrderByte, final byte highOrderByte)
  3. toInt(InetAddress address)
  4. toIntArray(byte[] byteArray)
  5. toIntArray(byte[] byteArray)
  6. toIntArray(float[] floatArray)
  7. toIntBuffer(int[] array)
  8. toIntFromByteArray(byte[] byteArray)
  9. toLittleEndian(int value)