How to print hexadecimal values from rfid card into characters on lcd [solved]

I am trying to print data from an rfid tag to an lcd display. the data is in hexadecimal(i think).
when i print it to my lcd, it shows all numbers and no text. please help me, i need to submit it quick.
Here's my code

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

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above

LiquidCrystal_I2C lcd(0x27,20,4);

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

//*****************************************************************************************//
void setup() {
  Serial.begin(9600);                                           // Initialize serial communications with the PC
  SPI.begin();                                                  // Init SPI bus
  mfrc522.PCD_Init();  
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
}

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

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

  //some variables we need
  byte block;
  byte len;
  MFRC522::StatusCode status;

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

  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

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



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



  //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));      //uncomment this to see all blocks in hex

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

  lcd.print(F("Name: "));

  byte buffer1[18];

  block = 2;
  len = 18;

  //------------------------------------------- GET FIRST NAME
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 2, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
  if (status != MFRC522::STATUS_OK) {
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  status = mfrc522.MIFARE_Read(block, buffer1, &len);
  if (status != MFRC522::STATUS_OK) {
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }

  //PRINT FIRST NAME
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      Serial.write(buffer1[i]);
      lcd.print(buffer1[i]);
    }
  }

  delay(3000);
  
lcd.clear();

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

Your array is of type byte - so the print will get numbers out.

If you want to see an hexadécimal string you need to check each value. If it’s between 0 and 9 then just print it but if it’s between 10 and 15 you need to print A to F.

Assuming all the bytes are in correct HEX range

if (buffer1[i] > 9) lcd.write('A' + buffer1[i]) - 10;
else lcd.print(buffer1[i]);

I want to print the hexadecimal value in text

You can also print the hex values of variables. Experiment with this:

Serial.print( myValue, HEX ) ;

You can also use sprintf() and do fancy formatting, including hex numbers, but it is more complex to use.

Oops

Oops indeed :slight_smile:
Need to leave some work to OP

1 Like

Thanks for replying so quickly on the forum, i think i need to be a little more accurate and specific with my question.
when i print 'buffer1[i]' to my lcd, i get numbers. I want text. i have stored the value 500 as in 500 points. this was for a credit card play system. instead of displaying 500 on the lcd, it is showing some numbers. Also, I want to know how i can compare and add values to the card for example, if this button pressed, then 500=500+1. I need a little help here as I am a new developer... Thanks.

int x;

x = 500;
lcd.setCursor(0,0);
lcd.print(x); // 500

x = x + 1;
lcd.setCursor(0,1);
lcd.print(x); // 501


Do you mean that buffer1 contains the ASCII characters for "500", or that the binary value of 500 is stored in buffer1? Since buffer1 is declared as a byte array, LCD.print will display the numeric value of the byte. If buffer1 contains ASCII characters, if there is no LCD.write() function you can cast to char when printing.

  //PRINT FIRST NAME
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] != 32)
    {
      Serial.write(buffer1[i]);
      lcd.print((char)buffer1[i]);
    }
  }

Yes thanks David, its working now. But all the empty spaces it is showing some lines... how do I get rid of that?

Likely problem is that there is some non-ASCII data being read off the rfid tag into buffer1. The code is checking for the ASCII code for space but any other non-printing character will give odd results on the LCD.
< edit >
You can try changing the if statement to skip over any non-printing characters, but there may still be some non ASCII content on the rfid tag.

  //PRINT FIRST NAME
  for (uint8_t i = 0; i < 16; i++)
  {
    if (buffer1[i] > 32) //do not print non-printing characters
    {
      Serial.write(buffer1[i]);
      lcd.print((char)buffer1[i]);
    }
  }

so how do i fix it?

Sorry if any trouble, but can you please post a fix code

I edited my previous reply #11 while you were replying.

Without knowing the exact contents of the rfid tag it is not possibly to give a complete answer.

if ( isascii(buffer1[i]) ) { ...

Perfect, it worked marvelously, thank you so much for your help, I really appreciate it.

That will be true for control characters, which will not print properly on an LCD. The isprint() function should work.

if (isprint(buffer1[i])) {

Yes, and you could use an 'else' clause with that, to print the unprintable characters as hex or decimal values for troubleshooting.

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