Microsoft Dynamics 365 Business Central – Base64 Encoding and Decoding

Base64 is a group of binary-to-text encoding schemes representing binary data in an ASCII string format. Base64 data encoding is designed to survive transport through transport layers that are not 8-bit clean, such as mail bodies. Encoding the binary data to ASCII text helps ensure that the data remains intact without loss or modification during transport.

The most common use case for base64 encoding is when binary data must be sent over text-based systems, such as email, HTTP, or complex data stored in XML or JSON.

The principle behind Base64 encoding takes 3 bytes of binary data and represents them as 4 ASCII characters. This increases the size of the data by 33%, which is a significant drawback, but sometimes this is acceptable given the benefits.

Base64 Encoding and Decoding with AL for Business Central via codeunit 4110 “Base64 Convert”. The “Base64 Convert” codeunit has many overload methods to convert text to and from its base-64 representation.

page 50100 "Base64 Encoding"
{
    ApplicationArea = All;
    Caption = 'Base64 Encoding';
    PageType = Card;

    layout
    {
        area(content)
        {
            group(General)
            {
                Caption = 'General';
                grid(Columns)
                {
                    ShowCaption = false;
                    group(text)
                    {
                        ShowCaption = false;
                        field(FromText; FromText)
                        {
                            ApplicationArea = All;
                            Caption = 'From Text';
                            Editable = true;
                            MultiLine = true;
                        }
                        field(ToText; ToText)
                        {
                            ApplicationArea = All;
                            Caption = 'To Text';
                            Editable = false;
                            MultiLine = true;
                        }
                    }
                }
            }
        }
    }
    actions
    {
        area(Promoted)
        {
            actionref(Encoderef; Encode)
            {
            }
            actionref(Dencoderef; Decode)
            {
            }
        }
        area(Processing)
        {
            action(Encode)
            {
                ApplicationArea = All;
                Caption = 'Encode';
                Image = EncryptionKeys;

                trigger OnAction()
                begin
                    ToText := Base64Convert.ToBase64(FromText);
                end;


            }
            action(Decode)
            {
                ApplicationArea = All;
                Caption = 'Decode';
                Image = Text;

                trigger OnAction()
                begin
                    ToText := Base64Convert.FromBase64(FromText);
                end;
            }
        }
    }

    var
        FromText: Text;
        ToText: Text;
        Base64Convert: Codeunit "Base64 Convert";
}

Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.

Leave a Reply

Your email address will not be published.