RFID array to string

Hello guys,

I have this code:

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?

hI sebastian,

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

        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

 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

1 Like

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