From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7844279822ce7c28 X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: Newbie question : types , representation Date: 1999/08/27 Message-ID: <8Ztx3.362$914.39388@typ11.nn.bcandid.com>#1/1 X-Deja-AN: 517746133 Content-Transfer-Encoding: 7bit References: <37BFC251.601ADF8F@village.uunet.be> <37C621F3.C6C0DC3A@acenet.com.au> Content-Type: text/plain; charset="iso-8859-1" X-Mimeole: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Trace: typ11.nn.bcandid.com 935750596 216.180.14.178 (Fri, 27 Aug 1999 06:43:16 EDT) MIME-Version: 1.0 NNTP-Posting-Date: Fri, 27 Aug 1999 06:43:16 EDT Newsgroups: comp.lang.ada Date: 1999-08-27T00:00:00+00:00 List-Id: Geoff Bull 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;