RC522 Serial prints UID, but can't save it

Hello Guys

I am desperately trying to save a UID of an RFID Card to a String.

At the moment, I've got this Code Snippet that works partly:

String content= "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
   Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
   Serial.print(mfrc522.uid.uidByte[i], HEX);
   content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
   content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
Serial.println("\n");
Serial.print("String: ");

content.toUpperCase();
String UID = content.substring(1);

Serial.println(UID);

Why it works partly? If I hold an RFID Card to the RC522 reader, the Serial prints the Card UID. The for loop is responsible for printing it. But the UID doesn't get saved in the Variable "UID", because it doesn't print it.

This is the Serial output:

 73 78 0A 43

String:

And that's it.

Now I wanted to ask you if I was missing something. I don't know if I'm not saving the UID in the content string the right way, or if I missed something else.

Regards
Dario

Have you by any chance got another variable named content in the code that you have not posted (HINT !)

Have you tried printing the length of content before printing UID ?

This is a simulation of your code as I don't have a card reader. The code behaves as it should

byte uidByte[] = {0x73, 0x78, 0x0A, 0x43};

void setup()
{
  Serial.begin(115200);


  String content = "";
  for (byte i = 0; i < sizeof(uidByte); i++)
  {
    Serial.print(uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(uidByte[i], HEX);
    content.concat(String(uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(uidByte[i], HEX));
  }
  Serial.println("\n");
  Serial.print("String: ");

  content.toUpperCase();
  String UID = content.substring(1);

  Serial.println(UID);
}

void loop()
{
}

Output

 73 78 0A 43

String: 73 78 0A 43

My conclusion is that the problem is not where you think it is.

Which board are you using?

Your topic is not storage related (see About the Storage category) and henced moved to a more suitable category on the forum.

The SafeString-library offers the print-function
So you can use the exact same code for filling the SafeString-variable

Anyway it would be a good idea to first extract the wanted bytes only one time to a variable
that is defined by your code and then use all the time this variable instead of mutliple calls to

mfrc522.WHAT_EVER_FUNCTION

because there is a risc that reading a different card will change the values inside the mfrc522-object

#include "SafeString.h"

byte uidByte[] = {0x73, 0x78, 0x0A, 0x43};

createSafeString(myContent,  16);

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}


void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();
  
  myContent = "";
  Serial.println();
  Serial.print("UID bytewise HEX ");
  
  for (byte i = 0; i < sizeof(uidByte); i++) {
    myContent.print(uidByte[i] < 0x10 ? " 0" : " ");
    myContent.print(uidByte[i], HEX);
    
    Serial.print(uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(uidByte[i], HEX);
  }
  
  Serial.println();
  Serial.println();
  
  Serial.print("myContent=#");
  Serial.print(myContent);
  Serial.println("#");
}


void loop() {
}

best regards Stefan

Hey Guys, thank you all for your responses!

With your help i was able to find a solution.
Instead of using String to save the UID, i used a unsigned long, and wrote a function to return the UID:

unsigned long getUID()
{
  status = NULL;
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 2, &key, &(mfrc522.uid)); 

  if (status != MFRC522::STATUS_OK)
  {
     return 0;
  }

  /* Reading data from the Block */
  status = mfrc522.MIFARE_Read(2, readBlockData, &bufferLen);
  if (status != MFRC522::STATUS_OK)
  {
    return 0;
  }
  else
  {    
    /*UID*/
    unsigned long tempUID[4] = {0};
    for (byte i = 0; i < mfrc522.uid.size; i++) tempUID[i] = mfrc522.uid.uidByte[i];

    volatile unsigned long UID = 0;
    UID |= (tempUID[0] << 24);
    UID |= (tempUID[1] << 16);
    UID |= tempUID[2] << 8;
    UID |= tempUID[3];

    return UID;
    /*UID*/
  }
}

Regards
Dario