Compare arrays in if else statement

I’m trying to compare arrays, one being an NFC tag number from the tag itself, the other being incoming data from the NFC reader. My code is as follows,

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_PN532.h"
#include "Particle.h"
#include "dct.h"
#include "string.h"
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]


#include <Adafruit_PN532.h>
#define PN532_IRQ   (A0)
#define PN532_RESET (A1)  // Not connected by default on the NFC Shield
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

int Sofia[34] = {0x04, 0x0F, 0x96, 0xD2, 0x4F, 0x5D, 0x80};
int Sof = D7;
int Moana[28] = {0x04, 0x1B, 0x96, 0xD2, 0x4F, 0x5D, 0x80};
int Moa = D8;
int Rapunzel[34] = {0x04, 0x27, 0x96, 0xD2, 0x4F, 0x5D, 0x80};
int Rap = D4;
int Ariel[34] = {0x04, 0x79, 0x8B, 0xD2, 0x4F, 0x5D, 0x80};
int Ari = D5;
int Elsa[34] = {0x04, 0x15, 0x96, 0xD2, 0x4F, 0x5D, 0x80};
int Els = D6;

void setup(void) {
    const uint8_t val = 0x01;
    dct_write_app_data(&val, DCT_SETUP_DONE_OFFSET, 1);


  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
      pinMode(Sof, INPUT_PULLUP);
  digitalWrite(Sof, HIGH);
  pinMode(Moa, INPUT_PULLUP);
  digitalWrite(Moa, HIGH);
  pinMode(Rap, INPUT_PULLUP);
  digitalWrite(Rap, HIGH);
  pinMode(Ari, INPUT_PULLUP);
  digitalWrite(Ari, HIGH);
  pinMode(Els, INPUT_PULLUP);
  digitalWrite(Els, HIGH);
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  // configure board to read RFID tags
  Serial.println("Waiting for an ISO14443A Card ...");

void setVolume(int volume)
  execute_CMD(0x06, 30); // Set the volume (0x00~0x30)

void execute_CMD(byte CMD, byte Par1) // Excecute the command and parameters
 // Calculate the checksum (2 bytes)
 int16_t checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1);

 // Build the command line
 byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, checksum >> 8, checksum & 0xFF, End_Byte};

 //Send the command line to the module
 for (byte k=0; k<10; k++)
  Serial1.write( Command_line[k]);

void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    if (uidLength == 4)
      // We probably have a Mifare Classic card ... 
      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      Serial.println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
	  // Start with block 4 (the first block of sector 1) since sector 0
	  // contains the manufacturer data and it's probably better just
	  // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
      if (success)
        Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16];
        // If you want to write something to block 4 to test with, uncomment
		// the following line and this text should be read back in a minute
        //memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);
        // success = nfc.mifareclassic_WriteDataBlock (4, data);

        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(4, data);
        if (success)
          // Data seems to have been read ... spit it out
          Serial.println("Reading Block 4:");
          nfc.PrintHexChar(data, 16);
          // Wait a bit before reading the card again
          Serial.println("Ooops ... unable to read the requested block.  Try another key?");
        Serial.println("Ooops ... authentication failed: Try another key?");
    if (uidLength == 7)
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        // Wait a bit before reading the card again
        Serial.println("Ooops ... unable to read the requested page!?");
    if (memcmp(uid, Sofia, 28) == 0) 
        execute_CMD (0x0F, 01);
        execute_CMD (0x03, 05);
  else if (memcmp(uid, Moana, 28) == 0)
        execute_CMD (0x0F, 01);
        execute_CMD (0x03, 03);
    else if (memcmp(uid, Rapunzel, 28) == 0)
        execute_CMD (0x0F, 01);
        execute_CMD (0x03, 04);
    else if (memcmp(uid, Ariel, 28) == 0)
        execute_CMD (0x0F, 01);
        execute_CMD (0x03, 01);
    else if (memcmp(uid, Elsa, 28) == 0)
        execute_CMD (0x0F, 01);
        execute_CMD (0x03, 02);

The NFC code was shared with me by @ScruffR as a reader and serial monitor for NFC. I’m pretty sure I have the syntax correct for memcmp, but I’m not sure where I may be going wrong.

That code is mostly not by me, I may add :wink:

You declare the cards as 34 or 28 integers but only initialise the first 7 of them.
You also compare on non-compatible arrays. uid[] is a byte array while your preset cards are integer arrays, consequently the individual fields of each array don’t allign and hance can’t compared via memcmp.
You are also comparing 28 bytes but your uid[] is only 7 bytes long, so you will always compare random bytes too.

  • you should change your preset cards from int[] to uint8_t[].
  • since you already know uid[] will always be 7 bytes be explicit by declaring it as such uint8_t uid[7] = { 0, 0, 0, 0, 0, 0, 0 };
  • only compare the amount of bytes you know to be valid (memcmp(uid, xxxx, sizeof(uid)))

When I have used MiFare Classic cards the UID is typically 4 bytes - this can be converted into a uint32_t or unsigned long card number. This then makes comparison and search through an array a bit easier.

card number = uid[0]<<24+uid[1]<<16+uid[2]<<8+uid[3];

If your cards are the 7 byte UID type then perhaps you can store as 2 uint32_t or unsigned long long (I haven’t tried this).

For a four byte card you can even do that

  uint_t number = 0x12345678;
  uint8_t uid[4] = { 0x12, 0x34, 0x56, 0x78 };
  if (number == *(uint32_t*)uid)

(this requires accounting for endianness on the number side - I haven’t tested or thought too deeply about that in above “pseudo code” - number initialisation may need reverting)

Or even simpler (now for a seven byte card with padding 0x00)

  uint8_t number[8] = { 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x00 };
  uint8_t uid[8]    = { 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x00 };
  if (*(uint64_t*)number == *(uint64_t*)uid)
1 Like

So comparing the integer array vs byte array makes perfect sense, I should have caught that glaring at me on the screen. The reason I have them longer than 7 was because of the syntax I read through several different tutorials showing:

int memcmp(const void *s1, const void *s2, size_t n);
Parameters or Arguments
An array to compare.
An array to compare.
The number of characters to compare.

Where it shows, “n” as the number of characters, not bytes, to compare. But again, this is for integers, not bytes, so that would never have been correct for how I am using it.

Also, @ScruffR I just meant that you shared it with me, not necessarily that it was yours. I ended up modifying it because it looked a lot cleaner than what I had written. Thanks for the input and lesson on arrays.

That is bad wording in that respective docu but since characters and bytes are exactly the same thing for the processor the difference in meaning is zero.

1 Like