I'm using RC522 to read RFID card. All is until if the HEX leading by 0 then i can't save it into String. But if i print it to Serial all is well. Here is my code:

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

#define RST_PIN         9
#define SS_PIN          10

String inStringHex = "";

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {

void loop() {
  // Look for new cards
  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
    //Serial.print("Card UID:");
    for (byte i = 0; i < 4; i++) {
      Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
      Serial.print(mfrc522.uid.uidByte[i], HEX);
      inStringHex += String(mfrc522.uid.uidByte[i], HEX);

    Serial.println(F("Hex normal"));

    inStringHex = "";

Here is what displayed in serial monitor: enter image description here

What i want is the String under "Hex normal" would be "04292e5a" not "4292e5a"

Please anyone help me. Thank You.

2 Answers 2


String(mfrc522.uid.uidByte[i], HEX) means you want to convert number into hexadecimal string repesentation, if you pass 4 as input parameter, it will convert it into "4", not "04". You need to apply similar approach as you have used when printing the value through UART. For example:

if (String(mfrc522.uid.uidByte[i] < 0x10) 
  inStringHex += " ";
inStringHex += String(mfrc522.uid.uidByte[i], HEX);

Personally, I would recommend switching from Strings into static buffers (because arduino has too small memory to use dynamic allocation and there could be issue with running out of memory due to memory fragmentation):

char id[16]; 
sprintf(id, "%02x%02x%02x%02x", mfrc522.uid.uidByte[0], mfrc522.uid.uidByte[1], mfrc522.uid.uidByte[2], mfrc522.uid.uidByte[3]);
  • Hi, i need to save into string because i will it convert it again to decimal. If i use your recommendation, can i still convert it to decimal?
    – Ian
    Apr 22, 2018 at 19:15
  • Yes of course, you will get the same string and for conversion you can use strtol with radix 16. But if you only want to convert four bytes into one long int, there is much simpler way
    – gabonator
    Apr 22, 2018 at 19:19
  • I will try your suggestion. Thank you sir.
    – Ian
    Apr 22, 2018 at 19:22
  • unsigned long result = 0; result |= (long)mfrc522.uid.uidByte[0] << 24; result |= (long)mfrc522.uid.uidByte[1] << 16; result |= (long)mfrc522.uid.uidByte[2] << 8; result |= (long)mfrc522.uid.uidByte[3];
    – gabonator
    Apr 22, 2018 at 19:23
  • Is d[0] taken from char id[16]?
    – Ian
    Apr 22, 2018 at 19:25

you can try ;

for (byte i=0; i < uidLength; i++)
  Serial.print(uid[i] < 0x10 ? " 0" : "");
  Serial.print(uid[i], HEX);
  if (uid[i] < 0x10) inStringHex += "0";
      inStringHex += String(uid[i], HEX);
} Serial.println(); Serial.println(inStringHex);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.