# Convert MFRC522 UID to string, byte array to string

**URL:** <https://forum.arduino.cc/t/convert-mfrc522-uid-to-string-byte-array-to-string/613945>\
**Category:** Programming\
**Created:** [October 5, 2019, 6:32pm UTC](https://forum.arduino.cc/t/convert-mfrc522-uid-to-string-byte-array-to-string/613945 "2019-10-05T18:32:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishnuanand](https://avatars.discourse-cdn.com/v4/letter/v/71e660/32.png) [@vishnuanand](https://forum.arduino.cc/u/vishnuanand)\
**Post date:** [October 5, 2019, 6:32pm UTC](https://forum.arduino.cc/t/convert-mfrc522-uid-to-string-byte-array-to-string/613945/1 "2019-10-05T18:32:12Z")

</div>

**Connection to Arduino**

![](https://europe1.discourse-cdn.com/arduino/original/4X/a/3/9/a39b8f3cf7ccbad878eeb954709d54902d2a7971.png)

**Complete Code**

```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';
}

```

**Output**

```cpp
Approximate your card to the reader...

8595A6A4

```

---

<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:** [May 6, 2021, 12:03am UTC](https://forum.arduino.cc/t/convert-mfrc522-uid-to-string-byte-array-to-string/613945/2 "2021-05-06T00:03:44Z")

</div>


