comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: Newbie question : types , representation
Date: 1999/08/27
Date: 1999-08-27T00:00:00+00:00	[thread overview]
Message-ID: <8Ztx3.362$914.39388@typ11.nn.bcandid.com> (raw)
In-Reply-To: 37C621F3.C6C0DC3A@acenet.com.au


Geoff Bull <gbull@acenet.com.au> wrote in message
news:37C621F3.C6C0DC3A@acenet.com.au...
>
>
> Jos De Laender wrote:
> >
>
> > In base64 decoding , each ASCII character must be translated to 6 'bit'.
> > 4 characters translate then to 3 bytes. The problem and algorithms are
> > trivial. In C I could do it within 5 minutes.
>
> The Ada only takes 5 minutes also:
>
> with Ada.Text_IO; use Ada.Text_IO;
>
Since the purpose of base64 encoding is usually to produce characters in
the printable set (i.e. Character'Val (32) .. Character'Val (95)), you
need to add 32 to each of your result values.

Also, to have a working program, one would need to deal with input
lengths not evenly divisible by 3.

Here's another approach:

with Ada.Unchecked_Conversion;
function Base64_Encoding (Input : String) return String
is
   type Bit is mod 2;
   for Bit'Size use 1;
   type Bit_Array is array (Positive range <>) of Bit;
   pragma Pack (Bit_Array);
   Input_Copy : String (1 .. 3 * ((Input'Length + 2) / 3));
   The_Bits : Bit_Array (1 .. 8 * Input_Copy'Length);
   for The_Bits'Address use Input_Copy'Address;
   Output : String (1 .. 4 * ((Input'Length + 3) / 4));
   type Mod_64 is mod 64;
   for Mod_64'Size use 6;
   type Six_Bits is array (1 .. 6) of Bit;
   pragma Pack (Six_Bits);
   function To_Mod_64 is new Ada.Unchecked_Conversion
     (Source => Six_Bits,
      Target => Mod_64);
begin
   Input_Copy (1 .. Input'Length ) := Input;
   -- Pad the input length with NUL characters to a length evenly
   -- divisible by 3.
   for C in Input'Length + 1 .. Input_Copy'Length loop
      Input_Copy (C) := Character'Val (0);
   end loop;
   for C in Output'Range loop
      Output (C) := Character'Val
        (32 + Integer
         (To_Mod_64 (Six_Bits (The_Bits (6 * C - 5  .. 6 * C)))));
   end loop;
   return Output;
end Base64_Encoding;







  parent reply	other threads:[~1999-08-27  0:00 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-08-22  0:00 Newbie question : types , representation Jos De Laender
1999-08-22  0:00 ` Wilhelm Spickermann
1999-08-23  0:00   ` Martin C. Carlisle
1999-08-23  0:00   ` Simon Wright
1999-08-22  0:00 ` Robert Dewar
1999-08-23  0:00   ` Wolfgang Jeltsch
1999-08-24  0:00     ` tmoran
1999-08-24  0:00   ` jdla
1999-08-24  0:00     ` Matthew Heaney
1999-08-24  0:00       ` Jos De Laender
1999-08-24  0:00         ` Brian Rogoff
1999-08-25  0:00           ` Jos De Laender
1999-08-22  0:00 ` Martin Dowie
     [not found] ` <37C621F3.C6C0DC3A@acenet.com.au>
1999-08-27  0:00   ` David C. Hoos, Sr. [this message]
1999-08-27  0:00   ` tmoran
1999-08-27  0:00     ` Florian Weimer
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox