Tuesday, 2 December 2014

NFC Mifare UID (Card Serial Number) reverse issue

If you are using NFC in your application, you should be careful while using UID of NFC card.

When any card is manufactured, it will be assigned a card serial number and it must be there if there is no any data.

When you try to fetch that UID using Android SDK NFC tag technology, it will return you UID in reverse order.

Generally when card ia manufactured, bytes are stored in LSB (Least Significant Byte) order while when Android read card serial number, it will read in MSB(Most Significant Byte) fashion.

For example. Any card has UID "12345678" then in MSB fashion, it will be like "78563412". 
So you need to convert this order from MSB to LSB.

Following short of code will help you to do this.

 String cardID = getHax(tag.getId());  
 String cardIdArr[] = cardId.split(" ");  
 String reverseCardId = "";  
 for (int i=0;i < cardIdArr.length;i++)  
   reverseCardId += cardIdArr[i];  
 Log.d("reverse"," card id = " + reverseCardId );