0

I need to convert an int array like this...

int[] myInts = {
    99999,
    9999,
    99999,
    1,
    1234567890,
    24385933,
    99
};

...as efficiently as possible into a byte[] so it can be saved on an NFC tag.

I have seen this very similar question from which I have tried the two the answers (here and here), but the byte arrays they create both have a length of 28.

Unfortunately, that's no good for me as I need to create a byte[] with a maximum length of 16 (as the tag this data has to be saved on is a MIFARE Ultralight EV1).

Is there any other approach I could try in order to convert myInts to a byte[16]?

Community
  • 1
  • 1
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • Each integer is 4 bytes. 7 x 4 = 28, the only way is if you know that some of those values are <=255 in which you could use 1 byte for that value. – cyroxis May 24 '16 at 19:25
  • Do you have any known constraints on the values of the array? For example, if you know that some of the elements of the array are within the range -128 to 127, you can store them in a single byte instead of 4 bytes. – Eran May 24 '16 at 19:26
  • 1
    The only way you can mathematically even hope to do this is if you can prove there are fewer than 2^(8 * 16)=2^128 possibilities for arrays you'll need to compress. If that's not possible, you're 100% out of luck, there is _no possible_ way you can do that much compression no matter how smart you are. – Louis Wasserman May 24 '16 at 19:32
  • You can get down to 19 bytes for this exact example if you write a series of [varints](https://developers.google.com/protocol-buffers/docs/encoding#varints). https://gist.github.com/anonymous/5160b46fc090773deb7896933ade8bc3 but it can as well get bigger than 7x4bytes a simple translation of int uses because this type of encoding is only small for positive numbers close to 0 but requires 5 bytes worst case per int. – zapl May 24 '16 at 19:52

1 Answers1

-1

Apologies. I got some info wrong in my question as according to the MIFARE Ultralight EV1 datasheet, the tag can hold 48 bytes of user data - which is good news. (I had been a bit mislead by NXP's example app which is hardcoded to just use 16 bytes.)

Thank you for the helpful comments anyway. :-)

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253