Search code examples
c++arduinonfcmifare

read mifare card block and compare part of its block with some text


I'm reading a Mifare card block using RC522, I wrote "RhytonUser001" in one block.

Arduino reads it in this way:

byte readbackblock[18];//This array is used for reading out a block. The MIFARE_Read method requires a buffer that is at least 18 bytes to hold the 16 bytes of a block.

for (int j=0 ; j<16 ; j++) //print the block contents
{
    char c = readbackblock[j];
    lcd.print(c);
}

readbackblock[j] is an ascii character that returns from card. I need to convert it to its real Characters, then convert it to String because i want to split that string.

I also tried this (without split):

     char d = readbackblock[0] + readbackblock[1] + readbackblock[2] + readbackblock[3] + readbackblock[4] + readbackblock[5];
     if(d == "Rhyton"){
      digitalWrite(7, HIGH);
     } else {
      digitalWrite(6, HIGH); // always this happens
     }

UPDATE:
example:
in above code please just think:
readbackblock[j] = 082 104 121 116 111 110 085 115 101 114 048 048 049
i can get it and convert it to char so it's become: RhytonUser001
then i want to split it String sth = split(***, 'User');
and get sth[0] and compare it with Rhyton to check if it is Rhyton or not
but when i try to do this i get an error. based on information given in this link I can not split char(because it is not string). So how can I compare readbackblock[j] with Rhyton or any other texts?


Solution

  • You can build a String object by concatenating the 16 characters.

    String strBlock = "";
    for (int j=0 ; j<16 ; j++) //print the block contents
    {
        char c = readbackblock[j];
        lcd.print(c);
        strBlock += c;
    }
    

    Then, you could use substring to extract various part.

    String strFirst6 = strBlock.substring( 0, 6 );
    if ( strFirst6 == "Rhyton" ) {
        ...
    

    Be cautious with the actual length of the string. It's undefined behavior if you try to read past the end. The above code suppose that none of the 16 characters will be 0.