I am trying to understand how a Mifare Ultralight Chip, I've got, works. This page pretty much explains it for normal circumstances: https://learn.adafruit.com/adafruit-pn532-rfid-nfc/ndef (At the very end of the page)
But now I have the problem that I want to write data on the chip that is bigger than 254 Byte. As an example, I am writing 493 'A's on it (With the 7 Byte Header, the Data will be 500 Byte overall). Here's what I get from reading the chip (the numbers are in decimal, not hex, so instead of '0xFF' it will say '255'):
0:________ 4 34 42 132 144 0
1:________186 144 107 129 144 0
2:________192 72 0 0 144 0
3:________225 16 109 0 144 0
4:________ 3 255 1 247 144 0
5:________193 1 0 0 144 0
6:________ 1 240 84 2 144 0
7:________101 110 65 65 144 0
8:________ 65 65 65 65 144 0 ...`
What I understand is, the first 4 blocks are the Header, which contains serial number, etc. Then the fifth block starts with a 3, which means "NDEF Message", and then I get the size.
For example, if I just write one 'A' on the chip, the given size will be 1 Byte payload + 7 Byte header = 8 Byte overall. So the 5th Block would look like this:
4:________ 3 8 209 1 144 0
(As the size doesn't exceed 254 byte, the 209 and 1 are not part of that size byte anymore)
but when I am writing 248 'A's, the overall size will be 255 bytes, which changes the 5th block to this:
4:________ 3 255 0 255 144 0
First I thought that this would indicate that I need to calculate x * y + z
where in this example, x = 255, y = 0 and z = 255, so 255 * 0 + 255 cause that would give me 255, the overall size, but when I add one 'A', so the size is 256 Bytes, the header changes to:
4:________ 3 255 1 0 144 0
This again would indicate that the calculation is x * y + y + z
, 255*1+1+0 = 256.
But then again, when I use 659 'A's to get 666 Byte, I get this:
4:________ 3 255 2 157 144 0
This again doesn't work for either of these algorithms:
255*2+157 = 667
255*2+2+157 = 669
And as a last example: 476 Bytes:
4:________ 3 255 1 223 144 0
255 * 1 + 223 = 478
255 * 1 + 1 + 223 = 479
Can anyone explain to me what I am doing wrong or why I just can't get the correct algorithm that leads me back to the Overall size?
I found it out. It's not about multiplying and adding, it's about looking at it in binary.
First, the first full byte isn't directly included in the calculation. What counts are the other two bytes.
In the 476 Byte Example, I got
4:________ 3 255 1 223 144 0
This means that I have to look at 1
and 223
in binary:
1 = 0000 0001
223 = 1101 1111
Now I have to merge them together:
0000 0001 1101 1111 = 479
If the overall size is over 259 Byte, 3 Extra bytes get added (idk why, but it's how my chip works. It uses 1 or 4 bytes, depending on size)
The other example were 666 + 3 Byte = 669:
4:________ 3 255 2 157 144 0
2 = 0000 0010
157 = 1001 1101
Total: 0000 0010 1001 1101 = 669