How to create and parse Tag, Length, Value (TLV) in .Net and encode it in Base64

2.1k Views Asked by At

The QR code fields shall be encoded in Tag-Length-Value (TLV) format with the tag values specified in the “Tag” column of the adjacent table.

The TLV encoding shall be as follows:

Tag: the tag value as mentioned above stored in one byte. Length: the length of the byte array resulted from the UTF8 encoding of the field value. The length shall be stored in one byte. Value: the byte array resulting from the UTF8 encoding of the field value.

Looking for Asp.net/Vb.net

Thanks in Advance

1

There are 1 best solutions below

0
Shawn Wright On

I was able to successfully create the TLV encoding in vb.net and it passed Zatca validation. I created a very simple form with 5 inputs: enter image description here

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim value1 As String = getTLV(1, TextBox1.Text)
        Dim value2 As String = getTLV(2, TextBox2.Text)
        Dim value3 As String = getTLV(3, TextBox3.Text)
        Dim value4 As String = getTLV(4, TextBox4.Text)
        Dim value5 As String = getTLV(5, TextBox5.Text)
        Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes(value1 & value2 & value3 & value4 & value5)
        Dim t As String = Convert.ToBase64String(b)
        TextBox6.Text = t
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Function getTLV(tag As Integer, value As String) As String
    Return Chr(tag) & Chr(value.Length) & value
End Function