# Adding display output to RFID HEX code print

**URL:** <https://forum.arduino.cc/t/adding-display-output-to-rfid-hex-code-print/1039611>\
**Category:** Programming\
**Created:** [October 8, 2022, 5:06am UTC](https://forum.arduino.cc/t/adding-display-output-to-rfid-hex-code-print/1039611 "2022-10-08T05:06:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mawalton910](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@mawalton910](https://forum.arduino.cc/u/mawalton910)\
**Post date:** [October 8, 2022, 5:06am UTC](https://forum.arduino.cc/t/adding-display-output-to-rfid-hex-code-print/1039611/1 "2022-10-08T05:06:50Z")

</div>

There is a thread here by @vishnuanand "Convert MFRC522 UID to string, byte array to string" amd since its closed I can no long comment. I am trying to add a display.print output to show the HEX UID on my ssd1306. I have been going at it for a couple hours and figured I would now seek some assistance.

I know its probably very simple but I am just missing it.

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

#define RST_PIN 9
#define SS_PIN 10

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  
void setup() {
   Serial.begin(9600);
   SPI.begin(); // Initiate SPI bus
   mfrc522.PCD_Init(); // Initiate MFRC522
   Serial.println("Approximate your card to the reader...");
}

void loop() {
   if ( ! mfrc522.PICC_IsNewCardPresent()) {
      return;
   }

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

   char str[32] = "";
   array_to_string(mfrc522.uid.uidByte, 4, str); //Insert (byte array, length, char array for output)
   Serial.println(str); //Print the output uid string
   mfrc522.PICC_HaltA();
}

void array_to_string(byte array[], unsigned int len, char buffer[])
{
   for (unsigned int i = 0; i < len; i++)
   {
      byte nib1 = (array[i] >> 4) & 0x0F;
      byte nib2 = (array[i] >> 0) & 0x0F;
      buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;
      buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA;
   }
   buffer[len*2] = '\0';
}

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [October 8, 2022, 5:20am UTC](https://forum.arduino.cc/t/adding-display-output-to-rfid-hex-code-print/1039611/2 "2022-10-08T05:20:35Z")

</div>

If you only want to print, you don't have to convert to a character array (assuming the display library that you use derives print functionality from the print class (which it more than likely does).

You can give below a try.

```cpp
for (unsigned int i = 0; i < 4; i++)
{
  if(mfrc522.uid.uidByte[i] < 0x10)
  {
    display.print("0");
    // for debugging
    Serial.print("0");
  }
  
  display.print(mfrc522.uid.uidByte[i], HEX); // thanks @johnwasser
  // for debugging
  Serial.print(mfrc522.uid.uidByte[i], HEX); // thanks @johnwasser
}

```

Use it instead of

```cpp
   array_to_string(mfrc522.uid.uidByte, 4, str); //Insert (byte array, length, char array for output)
   Serial.println(str); //Print the output uid string

```

Not compiled, not tested.

// edit:  
added the HEX to print in hexadecimal.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [October 8, 2022, 1:52pm UTC](https://forum.arduino.cc/t/adding-display-output-to-rfid-hex-code-print/1039611/3 "2022-10-08T13:52:40Z")

</div>

> [@sterretje](#):
>
> ```arduino
> display.print(mfrc522.uid.uidByte[i]);
> // for debugging
> Serial.print(mfrc522.uid.uidByte[i]);
> 
> ```

Don't forget the ", HEX"! 🙂

```arduino
  display.print(mfrc522.uid.uidByte[i], HEX);
  // for debugging
  Serial.print(mfrc522.uid.uidByte[i], HEX);

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [October 8, 2022, 2:42pm UTC](https://forum.arduino.cc/t/adding-display-output-to-rfid-hex-code-print/1039611/4 "2022-10-08T14:42:06Z")

</div>

> [@johnwasser](#):
>
> Don't forget the ", HEX"! 🙂

Thanks. Though you did vorrect it, I will update my post.

---

<div class="post-metadata">

**Author:** ![mawalton910](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@mawalton910](https://forum.arduino.cc/u/mawalton910)\
**Post date:** [October 8, 2022, 4:01pm UTC](https://forum.arduino.cc/t/adding-display-output-to-rfid-hex-code-print/1039611/5 "2022-10-08T16:01:59Z")

</div>

Thank you both.

I have it "working" I can see the code on my lcd. I ditched the oled for the lcd and it works better but now I need to get the display looking nice.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 6, 2023, 4:02pm UTC](https://forum.arduino.cc/t/adding-display-output-to-rfid-hex-code-print/1039611/6 "2023-04-06T16:02:32Z")

</div>

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