How to read bytes from a RFID card and convert them into an integer

I'm trying to save a integer on my RFID and then read it and subtract that integer. The problem is that I am able to serial print it but I want to be able to make it into an integer so I can subtract it and send a new value back to the RFID.

Here's the Code:
look and line 83's comment

#include <SPI.h>
#include <MFRC522.h>
int interger;
String str = "default";
String amountt;
#define RST_PIN         5           
#define SS_PIN          53           

MFRC522 mfrc522(SS_PIN, RST_PIN);   

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);  
  Serial.setTimeout(10);                                         
  SPI.begin();                                                 
  mfrc522.PCD_Init();                                              
  Serial.println(F("Read personal data on a MIFARE PICC:"));    
}

//*****************************************************************************************//
void loop() {

  
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

 
  byte block;
  byte len;
  MFRC522::StatusCode status;

  //-------------------------------------------

  
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

 
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  

  //-------------------------------------------

  mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid));

 

  //-------------------------------------------

  
  byte buffer1[18];


  block = 4;
  len = 18;


  //---------------------------------------- 
  
  byte buffer2[18];
  


  block = 1;

 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); 
  if (status != MFRC522::STATUS_OK) {
    
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer2, &len);
  if (status != MFRC522::STATUS_OK) {

    return;
  }
  
 for (uint8_t i = 0; i < 16; i++) {
  Serial.write(buffer2[i]);     // this is where my serial print is done but I want this into a integer
  }




interger = int.from_bytes(buffer2, byteorder='little');
  Serial.print(interger);


 

  //----------------------------------------

  
 
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}
//*****************************************************************************************//

Welcome to the forum

Bearing in mind that buffer2 is declared as an 18 byte array, how many digits are there in the integer ?

Two, the integer is 50 it shows up on the serial monitor but I want it as an integer.

Hi @gbric,

it looks as if you are using Python syntax to convert two bytes into an integer value. I assume that your sketch does not compile, does it?

If your data are stored as two byte little endian integers this is one method to convert the bytes into integer:

/*
  Forum: https://forum.arduino.cc/t/how-to-read-bytes-from-a-rfid-card-and-convert-them-into-an-integer/1158246
  Wokwi: https://wokwi.com/projects/373072344139685889

*/

byte buf[8] = {1, 0, 127, 0, 255, 0, 0, 1};


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Start");
  Serial.println(IntFromBytes(buf, 0));
  Serial.println(IntFromBytes(buf, 2));
  Serial.println(IntFromBytes(buf, 4));
  Serial.println(IntFromBytes(buf, 6));
  writeIntToBytes(20, 0);
  writeIntToBytes(40, 2);
  writeIntToBytes(120, 4);
  writeIntToBytes(512, 6);
  Serial.println(IntFromBytes(buf, 0));
  Serial.println(IntFromBytes(buf, 2));
  Serial.println(IntFromBytes(buf, 4));
  Serial.println(IntFromBytes(buf, 6));
}

void loop() {
  // put your main code here, to run repeatedly:

}

int IntFromBytes(byte *b, int offset) {
  return b[offset] + b[offset + 1] * 256;
}

void writeIntToBytes(int v, int offset) {
  buf[offset]   = v & 0xFF;
  buf[offset + 1] = v >> 8;
}

You can check this sketch on Wokwi: https://wokwi.com/projects/373072344139685889
if you like ...

If you do not like to address 0, 2, 4, 6, etc. you can of course change the index to 0, 1, 2, 3 etc. and let the code calculate the byte positions ...

Good luck!

P.S.: There was a reasonable input from @UKHeliBob: What data is your buffer really holding? Is it binary data as your code implies?

I tried the int from bytes but I just got random numbers.

12:27:40.273 -> -31488
12:27:40.273 -> 263
12:27:40.313 -> 19358

So it tired switching my for loop and putting into a string.

for (uint8_t i = 0; i < 16; i++) {
  str += buffer1[i];   
  }
Serial.print(str);

And I got this

11:43:28.360 -> 00013371900300139000

Is there a way to make this into my number 50?

Could you try this

for (uint8_t i = 0; i < 16; i++) {
  Serial.print(i);
  Serial.print("\t");
  Serial.println (buffer1[i], HEX);
  }

and post the results please?

I am confused as to whether or not your are reading data into buffer1 or buffer2. In the original code, buffer1 was unused.

byte buffer2[18];
block = 1;

 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid)); 
  if (status != MFRC522::STATUS_OK) {    
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer2, &len);
  if (status != MFRC522::STATUS_OK) {
    return;
  }
  
 for (uint8_t i = 0; i < 16; i++) {
  Serial.write(buffer2[i]);     // this is where my serial print is done but I want this into a integer
  }

You say this printed out "50". Were there any other characters trailing the 50?

You are declaring byte buffer1[18] and byte buffer2[18] locally, and they are not initialized with known values. They can contain anything which could confuse your efforts to print out what gets sent from the reader. It is better for you to fill them with known values before the card data is written into them.

16:22:04.134 -> 5	1
16:22:04.135 -> 6	8
16:22:04.167 -> 7	0
16:22:04.167 -> 8	0
16:22:04.167 -> 9	3
16:22:04.167 -> 10	0
16:22:04.167 -> 11	0
16:22:04.167 -> 12	8B
16:22:04.203 -> 13	0
16:22:04.203 -> 14	0
16:22:04.203 -> 15	0

Thanks, but it looks as if the first 5 values (0..4) are missing.The complete data would be required for serious analysis...

16:28:02.082 -> 0	0
16:28:02.082 -> 1	0
16:28:02.082 -> 2	0
16:28:02.082 -> 3	85
16:28:02.114 -> 4	7
16:28:02.114 -> 5	1
16:28:02.114 -> 6	8
16:28:02.114 -> 7	0
16:28:02.114 -> 8	0
16:28:02.114 -> 9	3
16:28:02.114 -> 10	0
16:28:02.147 -> 11	0
16:28:02.147 -> 12	8B
16:28:02.147 -> 13	0
16:28:02.147 -> 14	0
16:28:02.147 -> 15	0

Sorry it didn't all copy.

for (uint8_t i = 0; i < 16; i++) {
  Serial.write(buffer2[i]);     // this is where my serial print is done but I want this into a integer
  }

the integer is 50 it shows up on the serial monitor but I want it as an integer.

Can you still get 50 in the serial monitor when this is what you saw from the reader

|16:28:02.082 -> 0|0|
|---|---|
|16:28:02.082 -> 1|0|
|16:28:02.082 -> 2|0|
|16:28:02.082 -> 3|85|
|16:28:02.114 -> 4|7|
|16:28:02.114 -> 5|1|
|16:28:02.114 -> 6|8|
|16:28:02.114 -> 7|0|
|16:28:02.114 -> 8|0|
|16:28:02.114 -> 9|3|
|16:28:02.114 -> 10|0|
|16:28:02.147 -> 11|0|
|16:28:02.147 -> 12|8B|
|16:28:02.147 -> 13|0|
|16:28:02.147 -> 14|0|
|16:28:02.147 -> 15|0|

It does not seem feasible to interpret any of the data in buffer1 as "50"...

Please print the same but with buffer2 and post your most recent (complete) code:

for (uint8_t i = 0; i < 16; i++) {
  Serial.print(i);
  Serial.print("\t");
  Serial.println (buffer2[i], HEX);
  }

[edit] You may use this sketch here which is based on post #1 and post the results, starting with "Data in block " :

#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5
#define SS_PIN 53

constexpr byte BUFFERSIZE = 16;

byte block;
byte buffer[BUFFERSIZE];


MFRC522 mfrc522(SS_PIN, RST_PIN);

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);
  Serial.setTimeout(10);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println(F("Read personal data on a MIFARE PICC:"));
}

//*****************************************************************************************//
void loop() {
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  MFRC522::StatusCode status;
  //-------------------------------------------
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  //-------------------------------------------
  mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid));
 
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 1, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    return;
  }

// Clear buffer[]
  for (byte i = 0; i < BUFFERSIZE; i++) {
    buffer[i] = 0x00;
  } 

  status = mfrc522.MIFARE_Read(block, buffer, &BUFFERSIZE);
  if (status != MFRC522::STATUS_OK) {
    Serial.println("Error reading block!");
    return;
  }
// Dump buffer[]
  Serial.print(F("Data in block "));
  Serial.print(block);
  Serial.println(F(":"));
  dump_byte_array(buffer, BUFFERSIZE);
  Serial.println();

  //----------------------------------------

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}



/**
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buff, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buff[i] < 0x10 ? " 0" : " ");
    Serial.print(buff[i], HEX);
  }
}

Have not tested it "in real" but it compiles ...

It said

16:14:09.885 -> Error reading block!

Sorry the block no. was not set and the block number in Authenticate was hardcoded .. :slight_smile:

Try this please

#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 5
#define SS_PIN 53

constexpr byte BUFFERSIZE = 16;

byte block;
byte buffer[BUFFERSIZE];


MFRC522 mfrc522(SS_PIN, RST_PIN);

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);
  Serial.setTimeout(10);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println(F("Read personal data on a MIFARE PICC:"));
}

//*****************************************************************************************//
void loop() {
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  MFRC522::StatusCode status;
  //-------------------------------------------
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  //-------------------------------------------
  mfrc522.PICC_DumpDetailsToSerial(&(mfrc522.uid));
 
  block = 1;

  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    return;
  }

// Clear buffer[]
  for (byte i = 0; i < BUFFERSIZE; i++) {
    buffer[i] = 0x00;
  } 

  status = mfrc522.MIFARE_Read(block, buffer, &BUFFERSIZE);
  if (status != MFRC522::STATUS_OK) {
    Serial.println("Error reading block!");
    return;
  }
// Dump buffer[]
  Serial.print(F("Data in block "));
  Serial.print(block);
  Serial.println(F(":"));
  dump_byte_array(buffer, 16);
  Serial.println();

  //----------------------------------------

  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}



/**
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buff, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buff[i] < 0x10 ? " 0" : " ");
    Serial.print(buff[i], HEX);
  }
}

I prepared two example sketches both based on this tutorial

https://devcraze.com/tutorials/arduino/mfrc522-rfid-read-and-write-data-in-specific-rfid-block/

One sketch

  • to write a couple of characters to block 4 and integers to block 5 of a card

and the second one

  • to read the data from both blocks again.

Write example:

#include <SPI.h>
#include <MFRC522.h>

// For MEGA 
#define RST_PIN 5
#define SS_PIN 53

/* For UNO
#define RST_PIN 9                  // Configurable, see typical pin layout above
#define SS_PIN 10                  // Configurable, see typical pin layout above
*/
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;


enum States { WAITFORCARD,
             READCARD,
             WRITECARD };
States state = WAITFORCARD;

void setup() {
 Serial.begin(115200);  // Initialize serial communications with the PC
 SPI.begin();           // Init SPI bus
 mfrc522.PCD_Init();    // Init MFRC522 card
 Serial.println(F("Write data on a MIFARE PICC "));
}

void loop() {
 switch (state) {
   case WAITFORCARD:
     if (newCardFound()) {
       state = READCARD;
     }
     break;
   case READCARD:
     if (cardReadSuccessfully()) {
       state = WRITECARD;
     } else {
       Serial.println(F("Card could not be read!"));
       state = WAITFORCARD;
     }
     break;
   case WRITECARD:
     writeToBlockNoBytes(4);
     writeToBlockNoIntegers(5);
     // Further write functions can be placed here
     finalizeWriting();
     Serial.println(F("Present next card please"));
     state = WAITFORCARD;
     break;
 }
}

void finalizeWriting() {
 mfrc522.PICC_HaltA();
 mfrc522.PCD_StopCrypto1();
}

boolean newCardFound() {
 return mfrc522.PICC_IsNewCardPresent();
}

boolean cardReadSuccessfully() {
 return mfrc522.PICC_ReadCardSerial();
}

void writeToBlockNoBytes(byte block) {
 Serial.setTimeout(20000L);
 byte buffr[] = { 0x30, 0x31, 0x32, 0x33,
                  0x34, 0x35, 0x36, 0x37,
                  0x38, 0x39, 0x41, 0x42,
                  0x20, 0x20, 0x20, 0x20 };
 writeBytesToBlock(block, buffr);
 Serial.println(" ");
}

void writeToBlockNoIntegers(byte block) {
 Serial.setTimeout(20000L);
 int buffI[] = { -32768, -1000, -255, -1,
                 0, 255, 1000, 32767 };
 byte buffr[16];
 for (byte i=0;i<8;i++){
   buffr[i*2]   = buffI[i] & 0xFF;
   buffr[i*2+1] = buffI[i] >> 8;
 }
 writeBytesToBlock(block, buffr);
 Serial.println(" ");
}


void prepareKey() {
 // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
 for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
}

void writeBytesToBlock(byte block, byte buff[]) {
 prepareKey();
 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("PCD_Authenticate() failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 } else Serial.println(F("PCD_Authenticate() success: "));
 // Write block
 status = mfrc522.MIFARE_Write(block, buff, 16);
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("MIFARE_Write() failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 } else Serial.println(F("MIFARE_Write() success: "));
}

Read example:

#include <SPI.h>
#include <MFRC522.h>

// For MEGA 2560
#define RST_PIN 5
#define SS_PIN 53
/*
// For UNO
#define RST_PIN 9                  // Configurable, see typical pin layout above
#define SS_PIN 10                  // Configurable, see typical pin layout above
*/

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;

enum States {WAITFORCARD, READCARD, READANDPRINTDATA};
States state = WAITFORCARD;

//*****************************************************************************************//
void setup() {
  Serial.begin(115200);                               // Initialize serial communications with the PC
  SPI.begin();                                        // Init SPI bus
  mfrc522.PCD_Init();                                 // Init MFRC522 card
  Serial.print(F("Read block no. 4 (characters) and 5 (integers) "));
  Serial.println(F(" on a MIFARE PICC:"));  //shows in serial that it is ready to read
}
//*****************************************************************************************//
void loop() {
  switch(state){
    case WAITFORCARD:
       if (newCardDetected()){
         state = READCARD;
       }
      break;
     case READCARD :
         if (cardReadSuccessfully()) {
             state = READANDPRINTDATA;
         } else {
             Serial.println(F("Card could not be read!"));
             state = WAITFORCARD;
         }
      break;
    case READANDPRINTDATA:
        readAndPrintCharactersFromBlockNo(4);
        readAndPrintIntegersFromBlockNo(5);
        finalizeReading();
        state = WAITFORCARD;
        Serial.println(F("Present new card please"));
      break;


  }
}

void finalizeReading(){
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

boolean newCardDetected(){
   return mfrc522.PICC_IsNewCardPresent();
  }

boolean cardReadSuccessfully(){
  return mfrc522.PICC_ReadCardSerial();
  }

void prepareKey() {
  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
}

void readAndPrintCharactersFromBlockNo(byte block) {
  Serial.println(F("**Card Detected:**"));
  byte buffer1[18];
  byte len = 18;
  prepareKey();
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  String value = "";
  for (uint8_t i = 0; i < 16; i++) {
    value += (char)buffer1[i];
  }
  value.trim();
  Serial.print(value);
  Serial.println(F("\n**End Reading**\n"));
}

void readAndPrintIntegersFromBlockNo(byte block) {
  byte buffer1[18];
  byte len = 18;
  prepareKey();
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  int data[8];
  for (uint8_t i = 0; i < 8; i++) {
    data[i] = int(buffer1[2*i])+int(buffer1[2*i+1] << 8);
    Serial.print(data[i]);
    Serial.print('\t');
    if (i == 3) Serial.println();
  }
  Serial.println(F("\n**End Reading**\n"));
}

Both sketches are "hardwired" to block 4 and 5 just to show the principle.

I changed the sketches to work as a simple statemachine which I find easier to understand and follow the program flow (I do not really like the use of "return" in loop() ...).

This is the function that writes an array of integers to the card:

void writeToBlockNoIntegers(byte block) {
 Serial.setTimeout(20000L);
 int buffI[] = { -32768, -1000, -255, -1,
                 0, 255, 1000, 32767 };
 byte buffr[16];
 for (byte i=0;i<8;i++){
   buffr[i*2]   = buffI[i] & 0xFF;
   buffr[i*2+1] = buffI[i] >> 8;
 }
 writeBytesToBlock(block, buffr);
 Serial.println(" ");
}

As one can see each integer value is stored as low byte first and high byte in the second place.

This is the function that restores the integers from the byte array buffer1[ ]:

  int data[8];
  for (uint8_t i = 0; i < 8; i++) {
    data[i] = int(buffer1[2*i])+int(buffer1[2*i+1] << 8);
    Serial.print(data[i]);
    Serial.print('\t');
    if (i == 3) Serial.println();
  }

Each integer value is calculated by shifting the second byte (high byte) 8 times to the left and adding the first (low) byte. Whether you use little or big endian is finally up to you (if it does not depend on other applications that read the data).

These are the results you should see after writing and reading the data:

**Card Detected:**
0123456789AB
**End Reading**

-32768 -1000 -255 -1
0 255 1000 32767
**End Reading**

Present new card please

The difference between the two types in the read example is that block 4 data are interpreted as ASCII characters and block 5 data as integer.

I hope that the examples allow you to develop your application as intended.

Good luck!

I finally got it to work! Thank you and everyone that helped with this forum.

I adjusted the code a little bit and turned it into a variable integer that I can add and subtract to.

Write code edit

#include <SPI.h>
#include <MFRC522.h>

// For MEGA 
#define RST_PIN 5
#define SS_PIN 53

/* For UNO
#define RST_PIN 9                  // Configurable, see typical pin layout above
#define SS_PIN 10                  // Configurable, see typical pin layout above
*/
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;


enum States { WAITFORCARD,
             READCARD,
             WRITECARD };
States state = WAITFORCARD;

void setup() {
 Serial.begin(9600);  // Initialize serial communications with the PC
 SPI.begin();           // Init SPI bus
 mfrc522.PCD_Init();    // Init MFRC522 card
 Serial.println(F("Write data on a MIFARE PICC "));
}

void loop() {
 switch (state) {
   case WAITFORCARD:
     if (newCardFound()) {
       state = READCARD;
     }
     break;
   case READCARD:
     if (cardReadSuccessfully()) {
       state = WRITECARD;
     } else {
       Serial.println(F("Card could not be read!"));
       state = WAITFORCARD;
     }
     break;
   case WRITECARD:
     writeToBlockNoBytes(4);
     writeToBlockNoIntegers(5);
     // Further write functions can be placed here
     finalizeWriting();
     Serial.println(F("Present next card please"));
     state = WAITFORCARD;
     break;
 }
}

void finalizeWriting() {
 mfrc522.PICC_HaltA();
 mfrc522.PCD_StopCrypto1();
}

boolean newCardFound() {
 return mfrc522.PICC_IsNewCardPresent();
}

boolean cardReadSuccessfully() {
 return mfrc522.PICC_ReadCardSerial();
}

void writeToBlockNoBytes(byte block) {
 Serial.setTimeout(20000L);
 byte buffr[] = { 0x30, 0x31, 0x32, 0x33,
                  0x34, 0x35, 0x36, 0x37,
                  0x38, 0x39, 0x41, 0x42,
                  0x20, 0x20, 0x20, 0x20 };
 writeBytesToBlock(block, buffr);
 Serial.println(" ");
}

void writeToBlockNoIntegers(byte block) {
 Serial.setTimeout(20000L);
 int buffI[] = { 1 };  // enter the number here that you want on the card
 byte buffr[16];
 for (byte i=0;i<1;i++){
   buffr[i*2]   = buffI[i] & 0xFF;
   buffr[i*2+1] = buffI[i] >> 1;
 }
 writeBytesToBlock(block, buffr);
 Serial.println(" ");
}


void prepareKey() {
 // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
 for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
}

void writeBytesToBlock(byte block, byte buff[]) {
 prepareKey();
 status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("PCD_Authenticate() failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 } else Serial.println(F("PCD_Authenticate() success: "));
 // Write block
 status = mfrc522.MIFARE_Write(block, buff, 16);
 if (status != MFRC522::STATUS_OK) {
   Serial.print(F("MIFARE_Write() failed: "));
   Serial.println(mfrc522.GetStatusCodeName(status));
   return;
 } else Serial.println(F("MIFARE_Write() success: "));
}

read code edit

// number: variable is the card number and you able to treat it as a int
#include <SPI.h>
#include <MFRC522.h>

// For MEGA 2560
#define RST_PIN 5
#define SS_PIN 53
/*
// For UNO
#define RST_PIN 9                  // Configurable, see typical pin layout above
#define SS_PIN 10                  // Configurable, see typical pin layout above
*/

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
MFRC522::MIFARE_Key key;
MFRC522::StatusCode status;
int number;
enum States {WAITFORCARD, READCARD, READANDPRINTDATA};
States state = WAITFORCARD;

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);                               // Initialize serial communications with the PC
  SPI.begin();                                        // Init SPI bus
  mfrc522.PCD_Init();                                 // Init MFRC522 card
  Serial.print(F("Read block no. 4 (characters) and 5 (integers) "));
  Serial.println(F(" on a MIFARE PICC:"));  //shows in serial that it is ready to read
}
//*****************************************************************************************//
void loop() {
  switch(state){
    case WAITFORCARD:
       if (newCardDetected()){
         state = READCARD;
       }
      break;
     case READCARD :
         if (cardReadSuccessfully()) {
             state = READANDPRINTDATA;
         } else {
             Serial.println(F("Card could not be read!"));
             state = WAITFORCARD;
         }
      break;
    case READANDPRINTDATA:
        readAndPrintCharactersFromBlockNo(4);
        readAndPrintIntegersFromBlockNo(5);
        finalizeReading();
        state = WAITFORCARD;
        Serial.println(F("Present new card please"));
      break;


  }
}

void finalizeReading(){
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

boolean newCardDetected(){
   return mfrc522.PICC_IsNewCardPresent();
  }

boolean cardReadSuccessfully(){
  return mfrc522.PICC_ReadCardSerial();
  }

void prepareKey() {
  // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
}

void readAndPrintCharactersFromBlockNo(byte block) {
  Serial.println(F("**Card Detected:**"));
  byte buffer1[18];
  byte len = 18;
  prepareKey();
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  String value = "";
  for (uint8_t i = 0; i < 16; i++) {
    value += (char)buffer1[i];
  }
  value.trim();
  Serial.print(value);
  Serial.println(F("\n**End Reading**\n"));
}

void readAndPrintIntegersFromBlockNo(byte block) {
  byte buffer1[18];
  byte len = 18;
  prepareKey();
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Authentication failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.print(F("Reading failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  int data[1];
  for (uint8_t i = 0; i < 1; i++) {
    data[i] = int(buffer1[2*i])+int(buffer1[2*i+1] << 1);
    number = data[i];
    Serial.print('\t');
    if (i == 3) Serial.println();
  }
  Serial.println(number);
  Serial.println(F("\n**End Reading**\n"));
}

I put in the codes where to put your number to store on the card and stored it to a variable called number.

Once again thanks to everyone for all you help.