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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,13bebe8869a9207e X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-09-07 09:08:59 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsgate.cistron.nl!skynet.be!skynet.be!louie!tlk!not-for-mail Sender: lbrenta@lbrenta Newsgroups: comp.lang.ada Subject: Re: Octal Conversion in VB References: From: Ludovic Brenta Date: 07 Sep 2003 18:08:00 +0200 Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: -= Belgacom Usenet Service =- NNTP-Posting-Host: 217.136.11.50 X-Trace: 1062950939 reader2.news.skynet.be 459 217.136.11.50:46147 X-Complaints-To: usenet-abuse@skynet.be Xref: archiver1.google.com comp.lang.ada:42237 Date: 2003-09-07T18:08:00+02:00 List-Id: chrissy_brady1@yahoo.com (chrissy) writes: > I was playing around in VB trying to come up with a simple application ^^^^^^ See, comp.lang.ada? This illustrates my earlier point about disposable developers and disposable software. Chrissy, this is not against you, but why do you want a "simple" application? Is it not better to write a "correct" and "maintainable" application instead? > to convert entered strings of alphanumerics into octal numbers. > > This seemed to work, but the code looked ugly, and I'm not sure a > truth table would support the conversion to infinity. Am I right? > > Is there a better and tidier way? > > > Hoping for some guidance Here is a general algorithm that does what you want. Note that you need some mathematical background to understand why there are log(N)/log(8) octal digits in any integer number. HTH -- Ludovic Brenta. with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; procedure Decimal_To_Octal is function Read_Number return Integer is S : String (1 .. 100); Last : Natural; Result : Integer; begin Get_Line (S, Last); Get (From => S (1 .. Last), Item => Result, Last => Last); return Result; end Read_Number; Number_To_Convert : Integer := Read_Number; begin Put (Item => Number_To_Convert); -- First print it as decimal Put (Item => Number_To_Convert, Base => 8); -- Print as octal. Put_Line; -- An alternative that doesn't use the Ada representation for octals: declare Number_Of_Digits : constant Integer := 1 + Integer (Log (Float (Number_To_Convert)) / Log (8.0)); Octal_Digits : array (1 .. Number_Of_Digits) of Integer := (others => 0); begin for I in reverse Octal_Digits'Range loop Octal_Digits (I) := Number_To_Convert mod 8; Number_To_Convert := Number_To_Convert / 8; end loop; -- Now loop forward to print them for I in Octal_Digits'Range loop Put (Item => Octal_Digits (I), Width => 1); end loop; end; exception when Data_Error => Put_Line ("Please input a number in decimal."); end Decimal_To_Octal;