|

Read Bharat QR Data TLV Data to Tag and Extract Value

Parse Bharat QR QR using TLV format

Tag-Length-Value (TLV) is a data format used to encode information in a structured way. TLV is used in a wide variety of applications, including smart cards, payment systems, and networking protocols.

The TLV format consists of three components: the tag, the length, and the value. The tag is a unique identifier that specifies the type of data being encoded. The length indicates the number of bytes in the value field, and the value contains the actual data being encoded.

Here is a example of Bharat-QR TLV format data to be extracted in their tag format.

internal class Program
   {
       static void Main(string[] args)
       {
          
           Console.ReadLine();
       }

       public static void ExtractBharatQRData(string qrData)
       {
           qrData = "000201010212021647250010000000120415526550000000001061661000900000000310823ABCD000123812380151933726350010A0000005240110bivek@npci02031.027720010A0000005240135bivek1234567890123456789012345520130215www.npci.org.in28300010A000000524011270308093964452045411530335654032.05802IN5910Bivek Rath6006MUMBAI610640006762410203***0403***0603***0708000000030804test63047d4f";
           qrData = qrData.Replace(" ", "#");
           var data = TlvParser("tag", qrData, 0);
           // Furthure Tag Devision
           //26 Tag Devision
           if (data.TryGetValue("tag26", out string _value26))
           {
               var SubTag26 = TlvParser("tag", _value26, 0);
           }
           if (data.TryGetValue("tag27", out string _value27))
           {
               var SubTag27 = TlvParser("tag", _value27, 0);
           }
           if (data.TryGetValue("tag28", out string _value28))
           {
               var SubTag28 = TlvParser("tag", _value28, 0);
           }
       }
       public static Dictionary<string,string> TlvParser(string prefix,string RawData, int StartIndex)
       {
           var data= new Dictionary<string, string>();
           while(StartIndex!=RawData.Length)
           {
               string tagName=RawData.Substring(StartIndex,2);
               
               StartIndex =StartIndex+2;
               string Taglength=RawData.Substring(StartIndex,2);
               if(Int32.TryParse(Taglength, out int _taglengh))
               {
                   StartIndex = StartIndex + 2;
                   string tagValue=RawData.Substring(StartIndex, _taglengh);
                   StartIndex = StartIndex + _taglengh;

                   data.Add(prefix+tagName, tagValue);
               }
           }
           return data;
       }
   }

Output

You can pass the Tag name and get value.

Case of this program

  • While developing the UPI integration you will come across the scenario to read bharat qr and parsed the data and pass to TSP.
  • If you want to encode the data and pass to public and given then a specific guideline to read the read and create the data.
Spread the love

Similar Posts