Converting from byte to integer in arduino

hello guys,

I am writing code to perform calculations on the data stored in an RFID tag memory every time its scanned. the tag stores bytes and i would like to carry out the calculations in int data type. any help would be greatly appreciated. thanks in advance

Your post was MOVED to a more suitable location.

Could you also review How to get the best out of this forum as it will help you find your way around.

Why the need to calculate using the int data type when a byte is an integer ?

thanks for the reply , but actually the way i see it is like we save data to the byte array as a string. is there a way i could save an integer like data to the string or its already an integer even when its added in quotations like for strings.

Please post a full sketch that illustrates what you are doing and puts the questions in context

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

thanks very much for the guidance
here is my code

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

#define SS_PIN 10  /* Slave Select Pin */
#define RST_PIN 9  /* Reset Pin */

/* Create an instance of MFRC522 */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Create an instance of MIFARE_Key */
MFRC522::MIFARE_Key key;

int blockNum = 2;

byte blockDataAmount[5] = {"10000"}; //this is the amount of money stored on the card initially
byte blockDataPrice [5] = {"1000"};//this is the price per unit cut

byte blockDataBalance[5] = blockDataAmount[5] - blockDataPrice [5] ;  //and this is the amount of money balance left after the price per unit cut is subtracted from the initial amount above
// this balance is what is to be stored to the tag after scanning


MFRC522::StatusCode status;

void setup()
{
  /* Initialize serial communications with the PC */
  Serial.begin(9600);
  /* Initialize SPI bus */
  SPI.begin();
  /* Initialize MFRC522 Module */
  mfrc522.PCD_Init();
  Serial.println("Scan a MIFARE 1K Tag to write data...");
}

void loop()
{
  /* Prepare the key for authentication */
  /* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
  for (byte i = 0; i < 6; i++)
  {
    key.keyByte[i] = 0xFF;
  }
  /* Look for new cards */
  /* Reset the loop if no new card is present on RC522 Reader */
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }

  /* Select one of the cards */
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

  /* Call 'WriteDataToBlock' function, which will write data to the block */
  Serial.print("\n");
  Serial.println("Writing to Data Block...");
  WriteDataToBlock(blockNum, blockDataBalance);

}



void WriteDataToBlock(int blockNum, byte blockDataBalance[])
{
  /* Authenticating the desired data block for write access using Key A */
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Authentication failed for Write: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Authentication success");
  }


  /* Write data to the block */
  status = mfrc522.MIFARE_Write(blockNum, blockDataBalance, 16);
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Writing to Block failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Data was written into Block successfully");
  }

}

sorry about line 81, the figure of the array size is 5 not 16

I still don't know what you are trying to do apart from write to an RFID card

byte blockDataAmount[5] = {"10000"}; //this is the amount of money stored on the card initially
byte blockDataPrice [5] = {"1000"};//this is the price per unit cut

Neither of these declarations makes sense and I don't see where in the code you read from the card

about those declarations

i would like the first one to store the initial amount on the tag and the second one to store the price of an item that shall bve deducted from the card amount every time a tag is scanned.

I am not reading from the card in this particular code nut in a different one aside

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

#define SS_PIN 10  /* Slave Select Pin */
#define RST_PIN 9  /* Reset Pin */

/* Create an instance of MFRC522 */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Create an instance of MIFARE_Key */
MFRC522::MIFARE_Key key;
int blockNum = 2;
byte bufferLen = 8;
byte readBlockData[8];

MFRC522::StatusCode status;

void setup()
{
  /* Initialize serial communications with the PC */
  Serial.begin(9600);
  /* Initialize SPI bus */
  SPI.begin();
  /* Initialize MFRC522 Module */
  mfrc522.PCD_Init();
  //Serial.println("Scan a MIFARE 1K Tag to write data...");
  Serial.println("Scan a MIFARE 1K Tag to read data...");
}

void loop()
{
  /* Prepare the key for authentication */
  /* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
  for (byte i = 0; i < 6; i++)
  {
    key.keyByte[i] = 0xFF;
  }
  /* Look for new cards */
  /* Reset the loop if no new card is present on RC522 Reader */
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }

  /* Select one of the cards */
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }

  /* Read data from the same block */
  Serial.print("\n");
  Serial.println("Reading from Data Block...");

  ReadDataFromBlock(blockNum, readBlockData);

  /* Print the data read from block */
  Serial.print("\n");
  Serial.print("Data in Block:");
  Serial.print(blockNum);
  Serial.print(" --> ");
  for (int j = 0 ; j < 5 ; j++)
  {
    Serial.write(readBlockData[j]);
  }
  Serial.print("\n");
}

void ReadDataFromBlock(int blockNum, byte readBlockData[])
{
  /* Authenticating the desired data block for Read access using Key A */
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));

  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Authentication failed for Read: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Authentication success");
  }

  /* Reading data from the Block */
  status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Reading failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println("Block was read successfully");
  }

}

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