1

I am working on an Android application in which I am having some difficulty with converting byte Array to integer array.

I have a byte array and how I can convert byte array to integer array. I found a lot of solutions but most of them are not working properly. What is recommended way to convert byte array to integer array?

byte array

byte data[] = { (byte)0x80, (byte)0x1e, (byte)0x19, (byte)0x1e, (byte)0x06,(byte)0x1f,
                (byte)0x35,(byte)0x22,(byte)0x02,(byte)0x20,(byte)0x14,(byte)0x1e,(byte)0x37,
                (byte)0x1d,(byte)0x02,(byte)0x20,(byte)0x2e,(byte)0x1f,(byte)0x15,(byte)0x1e,
                (byte)0x38,(byte)0x00,(byte)0xff,(byte)0xfb,(byte)0xf8,(byte)0x00,(byte)0x01};

Thanks.

Arya
  • 1,729
  • 3
  • 17
  • 35

2 Answers2

1

There must be a more efficient way to do this, but the below is a solution.

Byte data[] = { (byte)0x80, (byte)0x1e, (byte)0x19, (byte)0x1e, (byte)0x06,(byte)0x1f,
                (byte)0x35,(byte)0x22,(byte)0x02,(byte)0x20,(byte)0x14,(byte)0x1e,(byte)0x37,
                (byte)0x1d,(byte)0x02,(byte)0x20,(byte)0x2e,(byte)0x1f,(byte)0x15,(byte)0x1e,
                (byte)0x38,(byte)0x00,(byte)0xff,(byte)0xfb,(byte)0xf8,(byte)0x00,(byte)0x01};

        int[] integers = new int[data.length];

        for(int i = 0;i < data.length ;i++){

            integers[i] = data[i];

        }
Gi0rgi0s
  • 1,757
  • 2
  • 22
  • 31
0

have you tried this?

byte[] arr = { 0x00, 0x01 };
ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default
short num = wrapped.getShort(); // 1

ByteBuffer dbuf = ByteBuffer.allocate(2);
dbuf.putShort(num);
byte[] bytes = dbuf.array(); // { 0, 1 }
sud
  • 505
  • 1
  • 4
  • 12