Go Down

Topic: Compare byte arrary with char array (RFID) (Read 1 time) previous topic - next topic


I have RFID and I get the UID with every reading. There are 4 bytes stored in array bytes. I have an SD card with the same brand and I want to consult in SD and validate. My problem: Uid is in byte array and my SD reading returns array of characters. I want to escape the String class. The reason I use SD and not eeprom is that I will have too much UID information on the card.

Any suggestions for comparing each byte of UID with the 8-character string in SD?

Thank you!


What does the code you've tried look like?


Look up the str**** group of functions



Here I read the UID.

Code: [Select]

const byte countTag = 5;       
unsigned char numTag[countTag] = {0};

void readTag(){
                    if ( ! mfrc522.PICC_IsNewCardPresent())
                      if ( ! mfrc522.PICC_ReadCardSerial()) 

                  for (byte i = 0; i < mfrc522.uid.size; i++)                 
                     numTag[i] = (mfrc522.uid.uidByte[i]);                     
                     Serial.print(F(" HEX: "));
                     Serial.println(numTag[i], HEX);       
  //With reading ok, the output for example: AA BB CC EE FF
                findUser ();

The problem for me is that uid comes 4 bytes. In SD the UID is obtained as 8 characters. They print well on serial monitor, but I do not see how to compare them to validate.

Excerpt I get uid from SD:

Code: [Select]

void findUser (){ 

        const byte numVectorUser =  50;
        char vetorUserTag[numVectorUser] =  {0};
        unsigned char index =   0;       
        char cr;   
        char numberTag[10] =  {0}; 
        myFile3 = SD.open("users.txt");
            cr = myFile3.read();         
            vectorUserTag[index] = cr;
              //"," indicate the end of string
  if(cr == ','){           

                // Data coming from SD: AABBCCDDEE               
                //Here I want to evaluate if UID (4 bytes read) is equal to my record in SD (8 characters coming).

thanks for your time


First you need to create a nul terminated character array
Code: [Select]
    if (cr == ',')
      // add nul terminator


Next you can use strtoul to convert the character array to an unsigned long (from your example data, I assume it's 4 bytes). Demo code below
Code: [Select]
char uuidFromfile[] = "AABBCCDD";

void setup()

  unsigned long uuid;
  char *endptr;

  uuid = strtoul(uuidFromfile, &endptr, 16);
  Serial.println(uuid, HEX);


Next you can compare against the data that you go from the reader; unfortunately an unsigned long is stored in reverse order from what we need, so we reverse the order of the bytes that were read from the reader
Code: [Select]

  // uuid bytes from reader
  byte uuidMFRC[4] = {0xaa, 0xbb, 0xcc, 0xdd};

  // temporary variable to hold reversed uuid
  byte tmp[4];
  for (int cnt = 0; cnt < 4; cnt++)
    tmp[3 - cnt] = uuidMFRC[cnt];

And next the actual compare
Code: [Select]
  // compare reversed uuidMFRC with numeric uuid from file
  if (memcmp(tmp, (byte*)&uuid, 4) == 0)
    Serial.println("no match");
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.


econjack, thanks for its time and response to the topic.

holmes4, his suggestion was the way.

sterretje, I understood your sketch and I will insert it in my need. Receive my thanks. :)

Go Up