comp.lang.ada
 help / color / mirror / Atom feed
From: chrissy_brady1@yahoo.com (chrissy)
Subject: Re: Octal Conversion in VB
Date: 7 Sep 2003 19:46:54 -0700
Date: 2003-09-08T02:46:55+00:00	[thread overview]
Message-ID: <f8f1b420.0309071846.3f2d1bb3@posting.google.com> (raw)
In-Reply-To: 3F5B91AA.4040203@attbi.com

"Robert I. Eachus" <rieachus@attbi.com> wrote in message news:<3F5B91AA.4040203@attbi.com>...
> Ludovic Brenta wrote:
> 
> > 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.
> 
> I think chrissy was actually looking to convert (ASCII?) characters into 
> octal triplets. Something more like what follows. 

I actually wanted to produce a string of alphanumerics representing
the integer value of a Base 10 integer, converted to Base 8. Thus the
ASCII string entered to a text box in VB "94" ultimately returns the
string "136".

Thanks so much for the suggestion. It's ingenious, but I'm stuck with
VB for the moment. Another friend suggested the following for VB:

Dim DecimalStr As String 
   Dim OctalStr As String 
   Dim HexadecimalStr As String 
   DecimalStr = InputBox ("Enter decimal value") 
   OctalStr = Oct$(DecimalStr) 
   HexadecimalStr = Hex$(DecimalStr) 
   MsgBox (OctalStr) 
   MsgBox (HexadecimalStr) 


As a safety mechanism to avoid problems with null values, you could
use these lines instead of the corresponding ones above:
   OctalStr = Oct$(DecimalStr & " ") 
   HexadecimalStr = Hex$(DecimalStr & " ") 

This of course does much more than I'd asked, as it's handling
hexadecimals as well.

The same friend also offered me a solution in Pascal:

 
 
 
I've attached a generic base conversion program in Pascal for bases
from 2 to 36 but there's no error handling. These are fairly standard
algorithms.  The programing term is 'cliches'. It converts from any
base to any base.

Louie. 

-----------------------------------------------------------------------------
Tabs set to 4 spaces
{ 
   baseconvert.pas     - Base convertor of integers 

       Luis Esteban    8 September 2003 
           Created without error checking 
} 

program BaseConvertor (input, output); 

   var 
       InputNumber     : string; 
       OutputNumber    : string; 
       InputBase       : integer; 
       OutputBase      : integer; 
       Value           : longint; 

function StringToInteger (Number : string; Base : integer) : longint; 

   var 
       Accumulator : longint; 
       i           : integer; 

   begin 
       Accumulator := 0; 

       for i := 1 to Length(Number) do 
           begin 
               {writeln ('Number[', i:1, '] = ', Number[i]);} 
               if Number[i] in ['0'..'9'] then 
                   begin 
                       Accumulator := Accumulator * Base +
(ord(Number[i]) - ord('0'));
                   end 
               else 
               if Number[i] in ['A'..'Z'] then 
                   begin 
                       Accumulator := Accumulator * Base + (10 +
ord(Number[i]) - ord('A'));
                   end 
               else 
               if Number[i] in ['a'..'z'] then 
                   begin 
                       Accumulator := Accumulator * Base + (10 +
ord(Number[i]) - ord('a'));
                   end 
               else 
                   begin 
                       writeln ('Ignoring ''', Number[i], ''''); 
                   end; 
           end; 
             StringToInteger := Accumulator; 
   end; 

function ReverseString (s : string) : string; 

   var 
       i   : integer; 
       t   : string; 
     begin 
       t := ''; 
       for i := Length(s) downto 1 do 
           begin 
               t := t + s[i]; 
           end; 
       ReverseString := t; 
   end; 

function IntegerToString (Number : longint; Base : integer) : string; 

   var 
       r           : string; 
       NewDigit    : integer; 
     begin 
       r := ''; 
       while Number > 0 do 
           begin 
               NewDigit := Number mod Base; 

               if NewDigit < 10 then 
                   begin 
                       r := r + chr(NewDigit+ord('0')); 
                   end 
               else 
               if NewDigit < 36 then 
                   begin 
                       r := r + chr(NewDigit+ord('A')-10); 
                   end; 
               Number := Number div Base; 
           end; 
       IntegerToString := ReverseString (r); 
   end; 

begin 
   write ('Enter number: '); 
   readln (InputNumber); 
   write ('Enter input base: '); 
   readln (InputBase); 
   write ('Enter output base: '); 
   readln (OutputBase); 
   writeln; 

   Value := StringToInteger(InputNumber, InputBase); 
   OutputNumber := IntegerToString(Value, OutputBase); 
   writeln (InputNumber,'(',InputBase,') = ',Value,'(10) =
',OutputNumber,'(',OutputBase,')');
end. 


Again this is more than I'd asked for. I'm so impressed with the
software community. You guys are just so generous!!


With Lots of Thanks and other Sincere signals of fratitude


Chrissy



      parent reply	other threads:[~2003-09-08  2:46 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
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 [this message]
replies disabled

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