-1

I need to retrieve an integer number for the selected bit range (1-128) of a Mifare Classic 1k block (16 byte array). The number is represented binary with Big Endian.

I know how to do it manually with bitwise operations for a given range but i'm not able to make a method to handle it. I have tried to use java.util.BitSet also but it works with little-endian representation.

Actual code:

private int byteToInt(byte[] payload, int from, int to) {
    BitSet bitSet = BitSet.valueOf(payload);
    byte[] array = bitSet.get(from,to).toByteArray();
    if(array.length == 0)
        return 0;
    else
        return new BigInteger(array).intValue();
}

The byte array on the BigInteger constructor is assumed to be big-endian byte-ordered while BitSet.toByteArray() returns a little-endian byte array

Visttux
  • 136
  • 6

2 Answers2

0

You need to convert the byte array into different endianness. Look here on how to convert little to big endian Converting Little Endian to Big Endian

and here for the other diection

Java - Convert Big-Endian to Little-Endian

Community
  • 1
  • 1
Guenther
  • 2,035
  • 2
  • 15
  • 20
-1

Something like this for big indane:-

int output = 0;
for (int index=0; index < size; index++)
{
    output = output | (arrary[index] << index);
}
Ramit
  • 416
  • 3
  • 8
  • output |= (arrary[index] << index); – Ramit Aug 12 '16 at 08:52
  • Thanks for accepting answer. BigInteger output = BigInteger.valueOf(0); in loop output = output.or(arrary[index] << (index*8)); guessing code you did. If not please share for others benefit – Ramit Aug 16 '16 at 17:07