title: How to Decode Base64 to TLV in PHP
body: <p>I am building an inhouse Invoicing solution for my company. The government requires us to create QR Code fields encoded in Tag-Length-Value (TLV) format.</p>
  <p>The TLV encoding shall be as follows:</p>
  <blockquote>
  <p>Tag: the tag value as mentioned above stored in one byte</p>
  </blockquote>
  <blockquote>
  <p>Length: the length of the byte array resulted from the UTF8 encoding of the field value. The length shall be stored in one byte.</p>
  </blockquote>
  <blockquote>
  <p>Value: the byte array resulting from the UTF8 encoding of the field value.</p>
  </blockquote>
  <p>In the example they have provided this Base64 Output</p>
  <pre><code>AQkxMjM0NTY3ODkCCjEyLzEyLzIwMjADBDEwMDADAzE1MPaIn2Z2jg6VqWvWV6IrZZNzLF7xvZrWXW5xRV5yFY2xFu0ycXOiyqV0k7Wsh6b1IcE2Tfzap1AQAQVsktmsv1FFQ1MxIAAAAGKblFMh9nFRSn8tvftXqo9zRSz2VEAPITSZ3W7UDHKhUx+7yXGijLtJSZGXMOc+jpKwARzDl68GmmRd75NWdOs=
  </code></pre>
  <p>When I decode using</p>
  <pre class="lang-php prettyprint-override"><code>$base64 = &quot;AQkxMjM0NTY3ODkCCjEyLzEyLzIwMjADBDEwMDADAzE1MPaIn2Z2jg6VqWvWV6IrZZNzLF7xvZrWXW5xRV5yFY2xFu0ycXOiyqV0k7Wsh6b1IcE2Tfzap1AQAQVsktmsv1FFQ1MxIAAAAGKblFMh9nFRSn8tvftXqo9zRSz2VEAPITSZ3W7UDHKhUx+7yXGijLtJSZGXMOc+jpKwARzDl68GmmRd75NWdOs=&quot;;
  echo base64_decode($base64); 
  </code></pre>
  <p>The response i get is:</p>
  <pre><code>    123456789
  12/12/20201000150���fv���k�W�+e�s,^��]nqE^r���2qs�ʥt������!�6M�ڧPl�٬�QECS1 b��S!�qQJ-��W��sE,�T@!4��n�r�S��q���II��0�&gt;���×��d]�Vt�
  </code></pre>
  <p>Tag 1: Seller's Name</p>
  <p>Tag 2: Vat Registration number</p>
  <p>Tag 3: Timestamp of the invoice</p>
  <p>Tag 4: Invoice Total</p>
  <p>Tag 5: Tax Total</p>
  <p>Tag 6: Hash of XML invoice</p>
  <p>Tag 7: ECDSA Signature</p>
  <p>Tag 8: ECDSA Public Key</p>
  <p>Tag 9: ECDSA signature of the cryptographic stamp’s public key</p>
  <p>What Am I doing wrong here and what is the right syntax to correct it?</p>