# Read and write rfid tags mfrc522

**URL:** <https://forum.arduino.cc/t/read-and-write-rfid-tags-mfrc522/895835>\
**Category:** General Guidance\
**Created:** [August 15, 2021, 3:51pm UTC](https://forum.arduino.cc/t/read-and-write-rfid-tags-mfrc522/895835 "2021-08-15T15:51:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![embedtronics\_raj](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@embedtronics\_raj](https://forum.arduino.cc/u/embedtronics_raj)\
**Post date:** [August 15, 2021, 3:51pm UTC](https://forum.arduino.cc/t/read-and-write-rfid-tags-mfrc522/895835/1 "2021-08-15T15:51:11Z")

</div>

hello, can someone help me out in getting the "readbackblock[j]" array output to integer  
this code is used reading and writing to rfid tag  
once, i get the output in integer, i need to increment and decrement the block value

the functions are in the code below the main code

```cpp

#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
int block = 10;
byte blockcontent[16] = {"5000"};
byte readbackblock[18];
void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("Scan a MIFARE Classic card");
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;//keyByte is defined in the "MIFARE_Key" 'struct' definition in the .h file of the library
  }

}

void loop()
{
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  Serial.println("card selected");

  Serial.println("Read before write");
  readBlock(block, readbackblock);
  Serial.print("read block: ");
  for (int j = 0 ; j < 16 ; j++)
  {
    Serial.write (readbackblock[j]);
  }
  Serial.println("");
  Serial.println("Write after Read");
  writeBlock(block, blockcontent);

  Serial.println("Read after Write");
  readBlock(block, readbackblock);
  Serial.print("read block: ");
  for (int j = 0 ; j < 16 ; j++)
  {
    Serial.write (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor
  }
  Serial.println("");
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

```

```cpp

       
int writeBlock(int blockNumber, byte arrayAddress[]) 
{
  //this makes sure that we only write into data blocks. Every 4th block is a trailer block for the access/security info.
  int largestModulo4Number=blockNumber/4*4;
  int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector
  if (blockNumber > 2 && (blockNumber+1)%4 == 0){Serial.print(blockNumber);Serial.println(" is a trailer block:");return 2;}//block number is a trailer block (modulo 4); quit and send error code 2
// Serial.print(blockNumber);
// Serial.println(" is a data block:");
 
  / *****************************************authentication of the desired block for access*********************************************************** /
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  //byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
  //this method is used to authenticate a certain block for writing or reading
  //command: See enumerations above -> PICC_CMD_MF_AUTH_KEY_A	= 0x60 (=1100000), // this command performs authentication with Key A
  //blockAddr is the number of the block from 0 to 15.
  //MIFARE_Key *key is a pointer to the MIFARE_Key struct defined above, this struct needs to be defined for each block. New cards have all A/B= FF FF FF FF FF FF
  //Uid *uid is a pointer to the UID struct that contains the user ID of the card.
  if (status != MFRC522::STATUS_OK) {
         Serial.print("PCD_Authenticate() failed: ");
         Serial.println(mfrc522.GetStatusCodeName(status));
         return 3;//return "3" as error message
  }
  //it appears the authentication needs to be made before every block read/write within a specific sector.
  //If a different sector is being authenticated access to the previous one is lost.

  / *****************************************writing the block*********************************************************** /
        
  status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16);//valueBlockA is the block number, MIFARE_Write(block number (0-15), byte array containing 16 values, number of bytes in block (=16))
  //status = mfrc522.MIFARE_Write(9, value1Block, 16);
  if (status != MFRC522::STATUS_OK) {
           Serial.print("MIFARE_Write() failed: ");
           Serial.println(mfrc522.GetStatusCodeName(status));
           return 4;//return "4" as error message
  }
  Serial.println("block was written");
}

int readBlock(int blockNumber, byte arrayAddress[]) 
{
  int largestModulo4Number=blockNumber/4*4;
  int trailerBlock=largestModulo4Number+3;//determine trailer block for the sector

  / *****************************************authentication of the desired block for access*********************************************************** /
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));
  //byte PCD_Authenticate(byte command, byte blockAddr, MIFARE_Key *key, Uid *uid);
  //this method is used to authenticate a certain block for writing or reading
  //command: See enumerations above -> PICC_CMD_MF_AUTH_KEY_A	= 0x60 (=1100000), // this command performs authentication with Key A
  //blockAddr is the number of the block from 0 to 15.
  //MIFARE_Key *key is a pointer to the MIFARE_Key struct defined above, this struct needs to be defined for each block. New cards have all A/B= FF FF FF FF FF FF
  //Uid *uid is a pointer to the UID struct that contains the user ID of the card.
  if (status != MFRC522::STATUS_OK) {
         Serial.print("PCD_Authenticate() failed (read): ");
         Serial.println(mfrc522.GetStatusCodeName(status));
         return 3;//return "3" as error message
  }
  //it appears the authentication needs to be made before every block read/write within a specific sector.
  //If a different sector is being authenticated access to the previous one is lost.

  / *****************************************reading a block*********************************************************** /
        
  byte buffersize = 18;//we need to define a variable with the read buffer size, since the MIFARE_Read method below needs a pointer to the variable that contains the size... 
  status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);//&buffersize is a pointer to the buffersize variable; MIFARE_Read requires a pointer instead of just a number
  if (status != MFRC522::STATUS_OK) {
          Serial.print("MIFARE_read() failed: ");
          Serial.println(mfrc522.GetStatusCodeName(status));
          return 4;//return "4" as error message
  }
  Serial.println("block was read");
}

```

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [August 15, 2021, 4:18pm UTC](https://forum.arduino.cc/t/read-and-write-rfid-tags-mfrc522/895835/2 "2021-08-15T16:18:29Z")

</div>

atoi() will convert your string "5000" to its integer value.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [August 15, 2021, 6:36pm UTC](https://forum.arduino.cc/t/read-and-write-rfid-tags-mfrc522/895835/3 "2021-08-15T18:36:52Z")

</div>

atoi will not work on 16 digit numbers 😉 One needs strtoull which does not exist in arduino land so you have to write it yourself.

@embedtronics_raj, google for _Arduino strtoull_, there is a stackoverflow post.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [August 16, 2021, 4:41am UTC](https://forum.arduino.cc/t/read-and-write-rfid-tags-mfrc522/895835/4 "2021-08-16T04:41:00Z")

</div>

It would presumably be more sensible to divide up the number in question.

It is not as if you are going to perform mathematics on it, it is purely arbitrary data.

You may think that in order to increment/ decrement it you need to use the whole number but you only need to increment/ decrement one **part** of it.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 14, 2021, 4:41am UTC](https://forum.arduino.cc/t/read-and-write-rfid-tags-mfrc522/895835/5 "2021-12-14T04:41:53Z")

</div>

Closed on December 14, 2021, 4:41am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
