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.
- Get character
- Get ASCII code of character
- Convert decimal to hex byte
- 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
Code | Character | Code | Character | Code | Character | Code | Character |
---|---|---|---|---|---|---|---|
0 | | 32 | [space] | 64 | @ | 96 | ` |
1 | | 33 | ! | 65 | A | 97 | a |
2 | | 34 | “ | 66 | B | 98 | b |
3 | | 35 | # | 67 | C | 99 | c |
4 | | 36 | $ | 68 | D | 100 | d |
5 | | 37 | % | 69 | E | 101 | e |
6 | | 38 | & | 70 | F | 102 | f |
7 | | 39 | ‘ | 71 | G | 103 | g |
8 | * * | 40 | ( | 72 | H | 104 | h |
9 | * * | 41 | ) | 73 | I | 105 | i |
10 | * * | 42 | * | 74 | J | 106 | j |
11 | | 43 | + | 75 | K | 107 | k |
12 | | 44 | , | 76 | L | 108 | l |
13 | * * | 45 | – | 77 | M | 109 | m |
14 | | 46 | . | 78 | N | 110 | n |
15 | | 47 | / | 79 | O | 111 | o |
16 | | 48 | 0 | 80 | P | 112 | p |
17 | | 49 | 1 | 81 | Q | 113 | q |
18 | | 50 | 2 | 82 | R | 114 | r |
19 | | 51 | 3 | 83 | S | 115 | s |
20 | | 52 | 4 | 84 | T | 116 | t |
21 | | 53 | 5 | 85 | U | 117 | u |
22 | | 54 | 6 | 86 | V | 118 | v |
23 | | 55 | 7 | 87 | W | 119 | w |
24 | | 56 | 8 | 88 | X | 120 | x |
25 | | 57 | 9 | 89 | Y | 121 | y |
26 | | 58 | : | 90 | Z | 122 | z |
27 | | 59 | ; | 91 | [ | 123 | { |
28 | | 60 | < | 92 | \ | 124 | | |
29 | | 61 | = | 93 | ] | 125 | } |
30 | | 62 | > | 94 | ^ | 126 | ~ |
31 | | 63 | ? | 95 | _ | 127 | |
PS: The values with blanks are control characters, not characters displayed or printed by Windows.
Code | Character | Code | Character | Code | Character | Code | Character |
---|---|---|---|---|---|---|---|
128 | € | 160 | [space] | 192 | À | 224 | à |
129 | 161 | ¡ | 193 | Á | 225 | á | |
130 | ‚ | 162 | ¢ | 194 | Â | 226 | â |
131 | ƒ | 163 | £ | 195 | Ã | 227 | ã |
132 | „ | 164 | ¤ | 196 | Ä | 228 | ä |
133 | … | 165 | ¥ | 197 | Å | 229 | å |
134 | † | 166 | ¦ | 198 | Æ | 230 | æ |
135 | ‡ | 167 | § | 199 | Ç | 231 | ç |
136 | ˆ | 168 | ¨ | 200 | È | 232 | è |
137 | ‰ | 169 | © | 201 | É | 233 | é |
138 | Š | 170 | ª | 202 | Ê | 234 | ê |
139 | ‹ | 171 | « | 203 | Ë | 235 | ë |
140 | Œ | 172 | ¬ | 204 | Ì | 236 | ì |
141 | 173 | | 205 | Í | 237 | í | |
142 | Ž | 174 | ® | 206 | Î | 238 | î |
143 | 175 | ¯ | 207 | Ï | 239 | ï | |
144 | 176 | ° | 208 | Ð | 240 | ð | |
145 | ‘ | 177 | ± | 209 | Ñ | 241 | ñ |
146 | ’ | 178 | ² | 210 | Ò | 242 | ò |
147 | “ | 179 | ³ | 211 | Ó | 243 | ó |
148 | ” | 180 | ´ | 212 | Ô | 244 | ô |
149 | • | 181 | µ | 213 | Õ | 245 | õ |
150 | – | 182 | ¶ | 214 | Ö | 246 | ö |
151 | — | 183 | · | 215 | × | 247 | ÷ |
152 | ˜ | 184 | ¸ | 216 | Ø | 248 | ø |
153 | ™ | 185 | ¹ | 217 | Ù | 249 | ù |
154 | š | 186 | º | 218 | Ú | 250 | ú |
155 | › | 187 | » | 219 | Û | 251 | û |
156 | œ | 188 | ¼ | 220 | Ü | 252 | ü |
157 | 189 | ½ | 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!!!😁
END
Hope this will help.
Thanks for reading.
ZHU
コメント