Base64 Encoding in Ada

Here’s the translated code and explanation in Ada, formatted in Markdown suitable for Hugo:

Ada provides built-in support for base64 encoding/decoding through the GNAT.Encode_Base64 and GNAT.Decode_Base64 packages.

with Ada.Text_IO;
with GNAT.Encode_Base64;
with GNAT.Decode_Base64;

procedure Base64_Encoding is
   use Ada.Text_IO;
   use GNAT.Encode_Base64;
   use GNAT.Decode_Base64;

   Data : constant String := "abc123!?$*&()'-=@~";
   Encoded : String (1 .. 4 * ((Data'Length + 2) / 3));
   Decoded : String (1 .. Data'Length);
   Last : Natural;
begin
   -- Here's the string we'll encode/decode.
   Put_Line ("Original: " & Data);

   -- Encode using the standard encoder
   Encode (Data, Encoded, Last);
   Put_Line ("Encoded: " & Encoded (1 .. Last));

   -- Decoding may raise an exception if the input is not well-formed.
   -- We use exception handling here.
   begin
      Decode (Encoded (1 .. Last), Decoded, Last);
      Put_Line ("Decoded: " & Decoded (1 .. Last));
   exception
      when others =>
         Put_Line ("Decoding failed");
   end;

   New_Line;

   -- Ada doesn't have a built-in URL-compatible base64 format.
   -- You would need to implement this yourself or use a third-party library.
   Put_Line ("Note: URL-compatible base64 is not built-in for Ada.");
end Base64_Encoding;

To run the program, save it as base64_encoding.adb and use the following commands:

$ gnatmake base64_encoding.adb
$ ./base64_encoding
Original: abc123!?$*&()'-=@~
Encoded: YWJjMTIzIT8kKiYoKSctPUB+
Decoded: abc123!?$*&()'-=@~

Note: URL-compatible base64 is not built-in for Ada.

Ada’s standard library provides base64 encoding and decoding through the GNAT packages. However, it doesn’t have a built-in URL-compatible base64 format. If you need URL-safe encoding, you would need to implement it yourself or use a third-party library.

The encoded string in Ada will be the same as the standard base64 encoding in other languages. However, Ada doesn’t provide a direct equivalent to the URL-safe encoding out of the box.