Home > Back-end >  E-Invoicing QR Code TLV with Base64 in C# , it only works if the company name is written in Latin le
E-Invoicing QR Code TLV with Base64 in C# , it only works if the company name is written in Latin le

Time:12-12

The attached code is working only if the company name is written in Latin letters but not working properly if the company name is in Arabic. Could anyone please advise and add the proper part to the code, I am a beginner in this field.

Tag-Length-Value (TLV) . • Code of the message type (T) - 1 Byte fixed sizes of 1 byte (2 digits). • Message value length (L) - 1 Byte fixed sizes of 1 byte (2 digits). • Message value itself. (V) - Variable (variable size) includes the result of encoding the text string into hexadecimal

Example of a TLV if the company name is written in Latin letters

String : Bobs Records3101223935000032021-12-10 01:26:44100.0015.00

Proper results: Hexadecimal: 010C426F6273205265636F726473020F3331303132323339333530303030330313323032312D31322D31302030313A32363A343404063130302E3030050531352E3030

HexToBase64: AQxCb2JzIFJlY29yZHMCDzMxMDEyMjM5MzUwMDAwMwMTMjAyMS0xMi0xMCAwMToyNjo0NAQGMTAwLjAwBQUxNS4wMA==

Decoded from Base64 format through https://www.base64decode.org :

Bobs Records 310122393500003 2021-12-10 01:26:44 100.00 15.00

=================================== Example of a TLV if the company name is written in Arabic letters:

String : شركة الأرض3101223935000032021-12-10 01:26:44100.0015.00

Improper results but don’t know where the issue is:

Hexadecimal: 010A3F3F3F3F203F3F3F3F3F020F3331303132323339333530303030330313323032312D31322D31302030313A32363A343404063130302E3030050531352E3030

HexToBase64: AQo/Pz8/ID8/Pz8/Ag8zMTAxMjIzOTM1MDAwMDMDEzIwMjEtMTItMTAgMDE6MjY6NDQEBjEwMC4wMAUFMTUuMDA=

Decoded from Base64 format through https://www.base64decode.org

???? ????? 310122393500003 2021-12-10 01:26:44 100.00 15.00

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace tlvgenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            string SallerName = gethexstring(1, "Bobs Records"); //Tag1
            string VATReg = gethexstring(2, "310122393500003"); //Tag2
            string DateTimeStr = gethexstring(3, "2022-04-25 15:30:00"); //Tage3
            string TotalAmt = gethexstring(4, "10000.00"); //Tag4
            string VatAmt = gethexstring(5, "150.00"); //Tag5
            string decString = SallerName   VATReg   DateTimeStr   TotalAmt   VatAmt;
            Console.WriteLine(decString);
            Console.WriteLine(HexToBase64(decString));
            Console.Read();


        }
        static string gethexstring(Int32 TagNo, string TagValue)
        {
            string StrTagNo = String.Format("0{0:X}", TagNo);
            String TagNoVal = StrTagNo.Substring(StrTagNo.Length - 2, 2);

            string StrTagValue_Length = String.Format("0{0:X}", TagValue.Length);
            String TagValue_LengthVal = StrTagValue_Length.Substring(StrTagValue_Length.Length - 2, 2);

            string decString = TagValue;
            byte[] bytes = Encoding.Default.GetBytes(decString);
            string hexString = BitConverter.ToString(bytes);
            hexString = TagNoVal   TagValue_LengthVal   hexString.Replace("-", "");

            return hexString;
        }

        static string gethexDec(Int32 TagValue)
        {
            string hxint = String.Format("0{0:X}", TagValue);
            return hxint.Substring(hxint.Length - 2, 2);

        }
        public static string HexToBase64(string strInput)
        {
            try
            {
                var bytes = new byte[strInput.Length / 2];
                for (var i = 0; i < bytes.Length; i  )
                {
                    bytes[i] = Convert.ToByte(strInput.Substring(i * 2, 2), 16);
                }
                return Convert.ToBase64String(bytes);
            }
            catch (Exception)
            {
                return "-1";
            }
        }

        private string StringToHex(string hexstring)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char t in hexstring)
            {
                //Note: X for upper, x for lower case letters
                sb.Append(Convert.ToInt32(t).ToString("x"));
            }
            return sb.ToString();
        }
    }
}

CodePudding user response:

To my knowledge Encoding.Default will return an encoding for the system's current ANSI code page. So in order to encode and decode anything else (like arabic letters) you should use Encoding.Unicode instead.

CodePudding user response:

   static string gethexstring(Int32 TagNo, string TagValue)
        {
          
            string decString = TagValue;
            byte[] bytes = Encoding.UTF8.GetBytes(decString);
            string hexString = BitConverter.ToString(bytes);

            string StrTagNo = String.Format("0{0:X}", TagNo);
            String TagNoVal = StrTagNo.Substring(StrTagNo.Length - 2, 2);

            string StrTagValue_Length = String.Format("0{0:X}", bytes.Length);
            String TagValue_LengthVal = StrTagValue_Length.Substring(StrTagValue_Length.Length - 2, 2);


            hexString = TagNoVal   TagValue_LengthVal   hexString.Replace("-", "");
            return hexString;
        }
  • Related