web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    If I understand it correctly, you should populate a byte array (System.Byte[]) and then send it to System.Convert::ToBase64String().

    If you aren't familiar with .NET Interop from X++, note that a very important piece of logic is catching CLR exceptions. If you don't do it and an exception occurs, users won't get any error message.

  • Ahmed Siliem Profile Picture
    508 on at

    Hi Mrtin,

    Thanks in advance for your prompt response.

    Sure, you understand it well.

    I got the first part of your response and have coded the coming code trying to encode the HEX string into base64. However, it does not result in the correct base64 string:

        System.Byte[]           byte;
        str                     HEX = "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",
                                str64;
        System.Text.Encoding    encoding = System.Text.Encoding::get_UTF8();
    
        // UTF8 input
        byte = encoding.GetBytes(HEX);
    
        // base64 Output
        str64 = System.Convert::ToBase64String(byte);
    
        // result
        info(strFmt("Hexadecimal input: %1", HEX));
        info(strFmt("base64: %1", str64));

    I am sure there is a mistake, but do not know where it is?

  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    According to you descriptions, the values 01 15 42 6f 62... are bytes. But your code doesn't respect that. You're working with the representation of bytes as if they were charactes in a string, and you takes bytes encoding this string, therefore you get something completely different. For example, character 0 is converted to binary data 0f (represented in hexa), 1 to to 31, space to 20 and so on. That's a completely different data.

    You could use your method if the bytes actually represented a text (which doesn't seem to be the case) and you converted them to the text before you using encoding.GetBytes().

    What you should do depends on what you actually get as an input.

    If your input is bytes, work with them as with bytes and forget all strings. Just add them to a byte array and them call ToBase64String().

    If you have a string with hexa code of bytes, you'll need to correctly interpret the substrings as bytes. For example, look at How do you convert a byte array to a hexadecimal string, and vice versa?.

  • Verified answer
    AL OJAIMI Profile Picture
    25 on at

    Hi Ahmed Siliem,

    I have the same  , I make workaround  bytes , please try 

    int i,j,k;

    str val;
    str valToConv;

    System.Byte byte ;
    System.Byte[] bytes ;
    System.Text.Encoding encoding = System.Text.Encoding::get_UTF8();

    valToConv = '0115426f627320426173656d656e74205265636f726473020f3130303032353930363730303030330314323032322d30342d32355431353a33303a30305a040a323130303130302e393905093331353031352e3135';
    val = '';
    k = strLen(valToConv)/2;
    bytes = encoding.GetBytes(subStr(valToConv,1,k));
    bytes.Clear();
    j=0;
    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);

    info(StrFmt('Value Converted to Base64: %1',val));

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    AL OJAIMI, please use Insert > Insert Code (in the rich formatting view) to paste source code. It preseves line indentation, which makes code much easier to read.

    int i,j,k;
    
    str val;
    str valToConv;
    
    System.Byte byte;
    System.Byte[] bytes;
    System.Text.Encoding encoding = System.Text.Encoding::get_UTF8();
    
    valToConv = '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);
    
    info(StrFmt('Value Converted to Base64: %1', val));

    Also, please separate tags by and don't prefix them with #.

  • Community Member Profile Picture
    on at

    @Ahmed Siliem

    can you plz share the code how you get this hexadecimal "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"

  • Community Member Profile Picture
    on at

    I have convert from Decimal to  hexadecimal through below code but now want to convert string to hexadecimal anyone can plz guide me in this regards thank you.

    ////DecToHex

    int64       val=21;

    str DecToHex = System.String::Format("{0:x}", val) ;

    info(strFmt('from decimle %1',DecToHex));

  • Suggested answer
    Ahmed Siliem Profile Picture
    508 on at

    Hi Usman,

    for the string of char you can use for loop with char2num to convert the string of char into numbers then int2Hex to convert the result into hex:

    int     L1,a;
    
    //To get the length of the string 
    L1 = strlen(your string);
    
    // to convert the string char into hex 
    for(a = 0; a <= L1; a  )
    {
        V1Hex  = int2Hex(char2num(your string,a));
    }

  • Ahmed Siliem Profile Picture
    508 on at

    @ Martin,

    Thanks for your usual support and valuable correction that led me in the right direction.

    @Mohamed,

    Thank you very much for your code as it shortened a lot of time.

    The last question in this series - if you permit me to add a related question here - is that a part of the values contains Non-Latin characters and therefore must be treated as UTF-8. So, how to directly convert from UTF-8 to hexadecimal?

    To make it more clear:

    the highlighted part below refers to the company name, sometimes it includes Non-Latin characters, I want to convert it directly from UTF-8 to hexadecimal.

    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

    To Encode from UTF8 to hexadecimal we can use an online tool such as Convert UTF8 to Hexadecimal - Online UTF8 Tools

    UTF8 Encoded values can be read using an online tool, i.e.: Convert Hexadecimal to UTF8 - Online UTF8 Tools


    I got it by C# but I do not know how to do that in X++

  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    Rewriting the logic with native X types is possible, but if you know how to do it in C#, you can do the same in X .

    X some limitations, e.g. it doesn't support generic methods, but workarounds usually exist. And if not, you can always create your own C# class (where you implement things not supported by X ) and call this class from X .

    For example, to convert a UTF-8 string to byte array, this is what you can do in C#:

    string s = "ščřžý";
    System.Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);

    And you can use exactly the same classes and methods in X :

    str s = 'ščřžý';
    System.Text.Encoding utf8 = System.Text.Encoding::UTF8;
    System.Byte[] bytes = utf8.GetBytes(s);

    I don't know which solution you're talking about when saying that you "got it by C#". Let me demonstrate the usage of BitConverter in X :

    str hexa = System.BitConverter::ToString(bytes);
    hexa = strReplace(hexa, '-', ' ');

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans