From: Ludovic Brenta <ludovic.brenta@skynet.be>
Subject: Re: Octal Conversion in VB
Date: 07 Sep 2003 18:08:00 +0200
Date: 2003-09-07T18:08:00+02:00 [thread overview]
Message-ID: <m37k4koa3j.fsf@insalien.org> (raw)
In-Reply-To: f8f1b420.0309070730.e42c6dd@posting.google.com
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;
next prev parent reply other threads:[~2003-09-07 16:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-09-07 15:30 Octal Conversion in VB chrissy
2003-09-07 16:08 ` Ludovic Brenta [this message]
2003-09-07 20:14 ` Robert I. Eachus
2003-09-07 20:22 ` David C. Hoos
2003-09-07 23:07 ` Robert I. Eachus
2003-09-08 2:46 ` chrissy
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox