# RFID array to string

**URL:** <https://forum.arduino.cc/t/rfid-array-to-string/662832>\
**Category:** Programming\
**Created:** [June 27, 2020, 10:32am UTC](https://forum.arduino.cc/t/rfid-array-to-string/662832 "2020-06-27T10:32:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastian-s](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@sebastian-s](https://forum.arduino.cc/u/sebastian-s)\
**Post date:** [June 27, 2020, 10:32am UTC](https://forum.arduino.cc/t/rfid-array-to-string/662832/1 "2020-06-27T10:32:02Z")

</div>

Hello guys,

I have this code:

```cpp
int getid(){

  if(!mfrc522.PICC_IsNewCardPresent()){
    return 0;
  }
  if(!mfrc522.PICC_ReadCardSerial()){
    return 0;
  }
  Serial.println("ID:");
  for(int i=0;i<4;i++){ //
    readcard[i]=mfrc522.uid.uidByte[i];
    array_to_string(readcard, 4, str);
    UserID = str;
  }
  mfrc522.PICC_HaltA();
  return 1;
}

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

```

Can someone explain to me what happened in these functions? Can you explain to me line by line?  
What are those values from function 2?

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [June 27, 2020, 7:40pm UTC](https://forum.arduino.cc/t/rfid-array-to-string/662832/2 "2020-06-27T19:40:13Z")

</div>

hI sebastian,

to explain every detail will take hundred lines  
to give you an overview

```arduino
        byte nib1 = (array[i] >> 4) & 0x0F;

```

take first byte of the array

> > 4 do a bitshift of four bits. This means the rightest four bits are thrown out  
> > & 0F seems somehow doubling to me. & is the bitwise and-operator.  
> > anybyte & 0000111 results in leftest four bits become zero

same with the other byte nib2

```arduino
 buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA;

```

if value of byte is lesser than hexadecimal "A" add ascii-code of character "0" else add ASCII-code of character "A"

after all the conversion is done the string contains the ID-number in hexadecimals

for example "1D0B9F03"

best regards Stefan

---

<div class="post-metadata">

**Author:** ![postmaster-ino](https://avatars.discourse-cdn.com/v4/letter/p/977dab/32.png) [@postmaster-ino](https://forum.arduino.cc/u/postmaster-ino)\
**Post date:** [June 27, 2020, 8:20pm UTC](https://forum.arduino.cc/t/rfid-array-to-string/662832/3 "2020-06-27T20:20:53Z")

</div>

value 0...F  
value + 0x30 ('0')  
value \>0x39? -\> value +7

---

<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:37am UTC](https://forum.arduino.cc/t/rfid-array-to-string/662832/4 "2021-05-06T00:37:07Z")

</div>


