Saving Leading Zero HEX into String Arduino

Status
Not open for further replies.

DianWardiana

Junior Member level 3
Joined
Sep 7, 2017
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
189
Hi,
I'm using Arduino Nano to read RFID card. I'm using RC522 as RFID Reader.
I have problem when i try to save RFID card value with leading zero into string. The leading zero is not saved into string.

Here is my code:
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() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
}

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();
    Serial.println(F("Hex normal"));
    Serial.print(inStringHex);
    Serial.println();
    Serial.println();

    inStringHex = "";
    delay(5000);
  }
}

Here is the result in Serial monitor:
https://imgur.com/a/0mbxlDH

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

Thank You.
 

You have to parse all special characters and add the scape comand \ right before them, as for example by replacing 0 with \0. This is necessary because many non-alphanumeric characters are used by the compiler as delimiters, therefore you need to instruct compiler to treat it as a bare character.
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…