Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to encode Tag-Length-Value (TLV) string into base64

(1) ShareShare
ReportReport
Posted on by 508

Hi Daxers,

For those who don't know what TLV is:

Type/Tag-Length-Value (TLV) is an encoding scheme used in many communication protocols to encode data. A TLV-encoded message has a defined structure that consists of 3 sections/parts. Those are:
● 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

The coming example shows a string of TLV and the required result of encoding it into base64:

Example of a TLV:

01 15 42 6f 62 73 20 42 61 73 65 6d 65 6e 74 20 52 65 63 6f 72 64 73 02 0f 31 30 30 30 32 35 39 30 36 37 30 30 30 30 33 03 14 32 30 32 32 2d 30 34 2d 32 35 54 31 35 3a 33 30 3a 30 30 5a 04 0a 32 31 30 30 31 30 30 2e 39 39 05 09 33 31 35 30 31 35 2e 31 35

Encode this to a Base64 must represent:

ARVCb2JzIEJhc2VtZW50IFJlY29yZHMCDzEwMDAyNTkwNjcwMDAwMwMUMjAyMi0wNC0yNVQxNTozMDowMFoECjIxMDAxMDAuOTkFCTMxNTAxNS4xNQ==

I got to know how to create the TLV and test it using TLV Utilities (emvlab.org).

Now, the question is, how to encode the previous TLV string into base64 programmatically in X++ to get the base64 string shown above??

I hope my question is clear and thank you in advance for your support.

Regards,

Ahmed

  • Suggested answer
    Ahmed Siliem Profile Picture
    508 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64
    [quote user="Rlakshmi"]

    This was helpful

    [/quote]

    Hi Rlakshmi,

    Thanks for your reply.

    I hope the tip I mentioned for encoding the Arabic string solved your problem.

    Regards,

    Ahmed

  • Rlakshmi Profile Picture
    5 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    This was helpful

  • Suggested answer
    dreamz Profile Picture
    10 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    Thanks Ahmed.

    Your support is greatly appreciated.

    Thanks

    Asif

  • Shahrukh Profile Picture
    501 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    Thanks Ahmed,

    The code is running absolutely fine after getting the size from byte string. i.e. bytesSellerName.

  • Suggested answer
    Ahmed Siliem Profile Picture
    508 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    Well, you'd better have the full code as the time limit is about to expire and the application will start tomorrow.

    You can change the fields that meet your requirements to replace the declared values:

    static void UTF8ToHexThenBase64(Args _args)
    {
        str NameAlias = 'شركة أ ب ج المحدودة', //Hamad M. ALDrees & Partners for Industry & Mining', //
            CoRegNum  = '300012345600003',
            DT        = '31/10/2021T01:05:10 pm',
            Total     = '268927.55',
            VatAmnt   = '35077.51',
            V1Hex,V2Hex,V3Hex,V4Hex,V5Hex,
            L1Hex,L2Hex,L3Hex,L4Hex,L5Hex,
            UTF8ComName;
    
        int L1,L2,L3,L4,L5,LCHex,a,j;
    
        Str1260 VHexString,VBase64String;
    
        ////To convert UTF8 strings into bytes
        System.Text.Encoding encoding = System.Text.Encoding::get_UTF8();
        System.Byte     byte;
        System.Byte[]   bytes1, bytes2, bytes3, bytes4, bytes5, bytesBase64;
    
        
        //// Get QR string
        ///Getting the LV of the first field
        //Convert the first UTF8 into byte array
        bytes1  = encoding.GetBytes(NameAlias);
        //Get the length of first UTF8 string in Hex
        L1Hex   = int2Hex(bytes1.get_Length(),2);
        //To convert bytes1 into hex
        V1Hex   = System.BitConverter::ToString(bytes1);
        //To elemenate the sign '-' (If any) from the resulted hex string
        V1Hex   = strReplace(V1Hex, "-", "");
    
        //Repeating the same with remaining fields
        bytes2 = encoding.GetBytes(CoRegNum);    
        L2Hex   = int2Hex(bytes2.get_Length(),2);
        V2Hex   = System.BitConverter::ToString(bytes2);
        V2Hex   = strReplace(V2Hex, "-", "");
    
        bytes3  = encoding.GetBytes(DT);
        L3Hex   = int2Hex(strlen(DT),2);
        V3Hex   = System.BitConverter::ToString(bytes3);
        V3Hex   = strReplace(V3Hex, "-", "");
        
        bytes4  = encoding.GetBytes(Total);
        L4Hex   = int2Hex(strlen(Total),2);
        V4Hex   = System.BitConverter::ToString(bytes4);
        V4Hex   = strReplace(V4Hex, "-", "");
        
        bytes5  = encoding.GetBytes(VatAmnt);
        L5Hex   = int2Hex(strlen(VatAmnt),2);
        V5Hex   = System.BitConverter::ToString(bytes5);
        V5Hex   = strReplace(V5Hex, "-", "");
        
        //GET the whole TLV string
        VHexString = '01'   L1Hex   V1Hex   '02'   L2Hex   V2Hex   '03'   L3Hex   V3Hex   '04'   L4Hex   V4Hex   '05'   L5Hex   V5Hex;
    
        //To convert the TLV string into Base64
        LCHex = strLen(VHexString) / 2;
        bytesBase64 = encoding.GetBytes(subStr(VHexString, 1, LCHex));
        bytesBase64.Clear();
    
        for (a = 1; a <= strLen(VHexString); a  )
        {
            byte = System.Convert::ToByte(hex2Int(subStr(VHexString, a, 2)));
            bytesBase64.SetValue(byte, j);
            a  ;
            j  ;
        }
        VBase64String = System.Convert::ToBase64String(bytesBase64);
    
        info(strFmt('TLV String: %1',VHexString));
        info(strFmt('Base64 string: %1',VBase64String));
    }

    I tried to shorten the code as much as possible to make it easier.

    I hope that code will help everyone. If anybody has an idea to enhance it more, please, share it with us.

    Yours,

    Ahmed Siliem

  • Suggested answer
    Ahmed Siliem Profile Picture
    508 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    Hi Naeem,

    The length of the original string works when it is in Latin since the length of the Latin text is fixed at UTF8, but the length of the Arabic text varies after it is converted to UTF8. So you have to work using the length of the converted UTF8 string to byte, not the length of the original text string.

    In the code from Asif, the bytesSellerName is the byte to store the converted UTF8 string. So, it is better to use:

    bytesSellerName.get_Length();

    Instead of the lenght of original string sellerName:

    strLen(sellerName);

    Hope this helps you,

    Regards,

    Ahmed

  • Shahrukh Profile Picture
    501 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    Hi Ahmed Siliem,

    Can you please tell us how exactly you were able to convert the string including Arabic name correctly?

    It would be very helpful for many of us if you post your complete method in this thread, which is working well for Arabic values.

  • Suggested answer
    Ahmed Siliem Profile Picture
    508 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    Hi Asif,

    Thank you for your participation and your attempt to enrich the topic.

    I apologize for saying that although the code is working, it is not correct, because it only works if the company name is written in Latin letters. Try the resulting finHex with the sellerName in Arabic here (it will give an error).

    Bearing in mind that what is required is that the sellerName must be in Arabic.

    Hint: You are converting the text to UTF8 and then Byte as an argument to convert to Hex, but it is not correct to go back to get the SNameL of the original text.

    Hoping this could help you.

    Regards,

    Ahmed

  • dreamz Profile Picture
    10 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    Thanks for this thread.

    I Hope below code will be helpful for creating TLV-Base64

    
    private str TLVHexaB64En(str sellerName , str VAT  , str  invDate , str invAmount , str TA    )
    {
        int a;
        str hexainvAmount;
        str hexaTA;
        str finHex;
    
        int i,j,k;
    
        str val;
        str valToConv;
    
        System.Byte byte;
        System.Byte[] bytes;
        System.Text.Encoding encoding = System.Text.Encoding::get_UTF8();
        System.Text.Encoding utf8 = System.Text.Encoding::get_UTF8();
    
        int  SNameL =  strLen(sellerName) ;
        int VATL =  strLen(VAT) ;
        int invDateL =  strLen(invDate) ;
        int invAmountL =  strLen(invAmount) ;
        int TAL =  strLen(TA) ;
    
        System.Byte[] bytesSellerName = utf8.GetBytes(sellerName);
        System.Byte[] bytesVAT = utf8.GetBytes(VAT);
        System.Byte[] bytesinvDate = utf8.GetBytes(invDate);
    
        str hexaT1 = "01";
        str hexaT2 = "02";
        str hexaT3 = "03";
        str hexaT4 = "04";
        str hexaT5 = "05";
    
        str hexaTL1 ;
        str hexaTL2 ;
        str hexaTL3 ;
        str hexaTL4 ;
        str hexaTL5 ;
    
        str hexaSellerName = System.BitConverter::ToString(bytesSellerName);
        str hexaVAT = System.BitConverter::ToString(bytesVAT);
        str hexainvDate = System.BitConverter::ToString(bytesinvDate);
    
        hexaTL1  = int2Hex(SNameL);
        hexaTL2  = int2Hex(VATL);
        hexaTL3  = int2Hex(invDateL);
        hexaTL4  = int2Hex(invAmountL);
        hexaTL5  = int2Hex(TAL);
        if (strLen(hexaTL1)==1)
        {
            hexaTL1 = "0" hexaTL1;
        }
          if (strLen(hexaTL2)==1)
        {
            hexaTL2 = "0" hexaTL2;
        }
    
        if (strLen(hexaTL3)==1)
        {
            hexaTL3 = "0" hexaTL3;
        }
    
        if (strLen(hexaTL4)==1)
        {
            hexaTL4 = "0" hexaTL4;
        }
    
        if (strLen(hexaTL5)==1)
        {
            hexaTL5 = "0" hexaTL5;
        }
    
    
    
        for( a = 0; a <= strLen(invAmount); a  )
        {
            hexainvAmount  = int2Hex(char2num(invAmount,a));
        }
    
         for( a = 0; a <= strLen(TA); a  )
        {
            hexaTA  = int2Hex(char2num(TA,a));
        }
    
        hexaSellerName = strReplace(hexaSellerName, '-', '');
        hexaVAT = strReplace(hexaVAT, '-', '');
        hexainvDate = strReplace(hexainvDate, '-', '');
    
        finHex = hexaT1 hexaTL1 hexaSellerName hexaT2 hexaTL2 hexaVAT hexaT3 hexaTL3 hexainvDate hexaT4 hexaTL4 hexainvAmount hexaT5 hexaTL5 hexaTA;
    
        valToConv = finHex;// '0115426f627320426173656d656e74205265636f726473020f3130303032353930363730303030330314323032322d30342d32355431353a33303a30305a040a323130303130302e393905093331353031352e3135';
        k = strLen(valToConv) / 2;
        bytes = encoding.GetBytes(subStr(valToConv, 1, k));
        bytes.Clear();
    
        for (i = 1; i <= strLen(valToConv); i  )
        {
            byte = System.Convert::ToByte(hex2Int(subStr(valToConv, i, 2)));
            bytes.SetValue(byte, j);
            i  ;
            j  ;
        }
    
        val = System.Convert::ToBase64String(bytes);
    
       return val;
    
    
    }
    
    
    
     

    Thanks

    Asif

  • Suggested answer
    Ahmed Siliem Profile Picture
    508 on at
    RE: How to encode Tag-Length-Value (TLV) string into base64

    Thank you very much Martin,

    You did it again with your guidance once you commented. I don't know, maybe it's the lack of sleep or the rush that leads me to despair. But after reading your last comment, I found what I wanted.

    Imagine that I was struggling with the line of code to convert the byte array into hex!

    Now it is ok.

    I am sure that this thread will help a lot of developers here in KSA who fight for the time before applying the e-invoice with QRCode.

    Thanks again.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,261 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,013 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans