Dynamics 365 Business Central: How to convert Text to Hexadecimal in AL

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to convert Text to Hexadecimal in AL.
The hexadecimal number system is a type of number system, that has a base value equal to 16. Hexadecimal numbers are represented by only 16 symbols. These symbols or values are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F.
Let’s look at a simple example, here is a tool I searched on Google.
Text To Hex: Business Central => 42 75 73 69 6E 65 73 73 20 43 65 6E 74 72 61 6C

How is this done? In simple terms, it requires four steps.

  1. Get character
  2. Get ASCII code of character
  3. Convert decimal to hex byte
  4. Continue with next character

Regarding Steps 1 and 2, we have mentioned them before when discussing how to extract characters from a string (indexing with []). For example,

Business Central => 66 117 115 105 110 101 115 115 32 67 101 110 116 114 97 108

I found some relevant datasheets in MS Learn (Docs). (Although there are not Business Central documents)
Character set (0 – 127): 7 bit

CodeCharacterCodeCharacterCodeCharacterCodeCharacter
032[space]64@96`
133!65A97a
23466B98b
335#67C99c
436$68D100d
537%69E101e
638&70F102f
73971G103g
8* *40(72H104h
9* *41)73I105i
10* *42*74J106j
1143+75K107k
1244,76L108l
13* *4577M109m
1446.78N110n
1547/79O111o
1648080P112p
1749181Q113q
1850282R114r
1951383S115s
2052484T116t
2153585U117u
2254686V118v
2355787W119w
2456888X120x
2557989Y121y
2658:90Z122z
2759;91[123{
2860<92\124|
2961=93]125}
3062>94^126~
3163?95_127

PS: The values with blanks are control characters, not characters displayed or printed by Windows.

Character set (128 – 255):

CodeCharacterCodeCharacterCodeCharacterCodeCharacter
128160[space]192À224à
129161¡193Á225á
130162¢194Â226â
131ƒ163£195Ã227ã
132164¤196Ä228ä
133165¥197Å229å
134166¦198Æ230æ
135167§199Ç231ç
136ˆ168¨200È232è
137169©201É233é
138Š170ª202Ê234ê
139171«203Ë235ë
140Œ172¬204Ì236ì
141173­205Í237í
142Ž174®206Î238î
143175¯207Ï239ï
144176°208Ð240ð
145177±209Ñ241ñ
146178²210Ò242ò
147179³211Ó243ó
148180´212Ô244ô
149181µ213Õ245õ
150182214Ö246ö
151183·215×247÷
152˜184¸216Ø248ø
153185¹217Ù249ù
154š186º218Ú250ú
155187»219Û251û
156œ188¼220Ü252ü
157189½221Ý253ý
158ž190¾222Þ254þ
159Ÿ191¿223ß255ÿ

PS:
1. Character 160 is a no-break space. Character 173 is a soft hyphen. Some characters aren’t supported by Microsoft Windows (characters 129, 141, 143, 144, and 157).

2. The values in the table are the Windows default. However, values in the ANSI character set above 127 are determined by the code page specific to your operating system.

Then convert it to hexadecimal, so the complete process is as follows:

  • B => 66 = 4*161+2*160 = 42
  • u => 117 = 7*161+5*160 = 75
  • s => 115 = 7*161+3*160 = 73
  • i => 105 = 6*161+9*160 = 69
  • n => 110 = 6*161+14*160 = 6E
  • e => 101 = 6*161+5*160 = 65
  • s => 115 = 7*161+3*160 = 73
  • s => 115 = 7*161+3*160 = 73
  • [space] => 32 = 2*161+0*160 = 20
  • C => 67 = 4*161+3*160 = 43
  • e => 101 = 6*161+5*160 = 65
  • n => 110 = 6*161+E*160 = 6E
  • t => 116 = 7*161+4*160 = 74
  • r => 114 = 7*161+2*160 = 72
  • a => 97 = 6*161+1*160 = 61
  • l => 108 = 6*161+12*160 = 6C

The next step is how to implement it in AL.
The first approach is to do all the coding yourself, which is also a practice that comes from the NAV.

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

page 50200 "Convert Text To Hexadecimal"
{
    PageType = Card;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(Input)
            {
                field(InputText; InputText)
                {
                    Caption = 'Input Text';
                    ApplicationArea = All;
                    trigger OnValidate()
                    begin
                        if InputText = '' then
                            Hexadecimal := '';
                        Hexadecimal := ConvertTextToHexadecimal(InputText);
                    end;
                }
            }
            group(Result)
            {
                field(Hexadecimal; Hexadecimal)
                {
                    Caption = 'Hexadecimal';
                    ApplicationArea = All;
                    Editable = false;
                }
            }
        }
    }
    var
        InputText: Text;
        Hexadecimal: Text;

    local procedure ConvertTextToHexadecimal(Input: Text) Output: Text
    var
        i: Integer;
        CharNo: Integer;
        HexLeft: Integer;
        HexRight: Integer;
    begin
        for i := 1 to StrLen(Input) do begin
            CharNo := Input[i];
            HexLeft := Round(CharNo / 16, 1, '<');
            HexRight := CharNo mod 16;
            Output += HexValue(HexLeft) + HexValue(HexRight);
        end;
    end;

    local procedure HexValue(Value: Integer): Text
    begin
        case Value of
            0 .. 9:
                exit(Format(Value));
            10:
                exit('A');
            11:
                exit('B');
            12:
                exit('C');
            13:
                exit('D');
            14:
                exit('E');
            15:
                exit('F');
        end;
    end;
}

There is actually a second way. In codeunit 10 “Type Helper”, you can find a method, procedure IntToHex.

    procedure IntToHex(IntValue: Integer): Text
    var
        DotNetIntPtr: DotNet IntPtr;
    begin
        DotNetIntPtr := DotNetIntPtr.IntPtr(IntValue);
        exit(DotNetIntPtr.ToString('X'));
    end;

Yes, we can simply use it.

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

page 50200 "Convert Text To Hexadecimal"
{
    PageType = Card;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(Input)
            {
                field(InputText; InputText)
                {
                    Caption = 'Input Text';
                    ApplicationArea = All;
                    trigger OnValidate()
                    begin
                        if InputText = '' then
                            Hexadecimal := '';
                        Hexadecimal := ConvertTextToHexadecimal(InputText);
                    end;
                }
            }
            group(Result)
            {
                field(Hexadecimal; Hexadecimal)
                {
                    Caption = 'Hexadecimal';
                    ApplicationArea = All;
                    Editable = false;
                }
            }
        }
    }
    var
        InputText: Text;
        Hexadecimal: Text;

    local procedure ConvertTextToHexadecimal(Input: Text) Output: Text
    var
        i: Integer;
        CharNo: Integer;
        TypeHelper: Codeunit "Type Helper";
        Str: Text;
    begin
        for i := 1 to StrLen(Input) do begin
            CharNo := Input[i];
            Str := TypeHelper.IntToHex(CharNo);
            Output += Str;
        end;
    end;
}

Great. Give it a try!!!😁

PS: Dynamics 365 Business Central: How to prohibit users from entering illegal characters such as emoji, Hiragana, kanji, etc.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL