Search code examples
androidbytenfcmifare

android byte array is shortening the numbers?


In my little NFC related project i'm trying to set a byte[] as authentication key for the Mifare Classic 1k chips i'm using. When is set it like this:

private byte[] key = {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff};

it ends up being -1 -1 -1 -1 -1 -1... Why is Android translating it like this, and, how should it really be coded?


Solution

  • This has nothing to do with Android or NFC, this is is how Java works. byte's are signed, which means that one of the bits is used as the sign. 0xff=11111111, which happens to mean -1 for Java. Your MIFARE card doesn't care about this though, so your program should work fine.